Sunday, October 5, 2014

Showing the error messages for both system validation and custom validaton at a time in Oracle ADF

Sometimes we have the requirement to show the custom error messages coupled with the framework related error messages simultaneously on the screen.

Using the FacesMessage will show the error message for just one component and if we don't put the immediate="true" for the button, then just the system validations/framework validations are shown on the screen. But we want to display both the system messages and custom error messages by setting the immediate="true" for the button.

The following code snippet will solve the problem

Command Button Code
 
    <af:commandButton text="Submit" id="cb1" immediate="true"
                      actionListener="#{pageFlowScope.ErrorBean.onClickSubmit}">
      <af:clientAttribute name="superImmediate" value="#{true}"/>
      <af:clientListener method="catchActionEvent" type="action"/>
      <af:serverListener type="customButtonAction"
                         method="#{pageFlowScope.ErrorBean.customButtonActionListener}"/>
    </af:commandButton>

Javascript code to be embedded in the Jsff

<af:resource type="javascript">  
   AdfActionEvent.prototype.isValidationNeeded = function () {
       return !this.getSource().getProperty("superImmediate");
   }

   // Action event will cause server-side validation at the 
   // Apply Request Values phase (for immediate button). 
   // So we'll get validation error even if the client-side validation is suppressed 
   // We need to catch the original Action event, cancel it and replace with our 
   // custom event customButtonAction. 
   function catchActionEvent(evt) {
       AdfCustomEvent.queue(evt.getSource(), 'customButtonAction', null, true);
       evt.cancel();
   }
</af:resource>

Backing bean Code

public void onClickSubmit(ActionEvent actionEvent) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error while Saving","Error while saving");
    facesContext.addMessage(null,msg);
}

public void customButtonActionListener(ClientEvent clientEvent) { 
      UIXCommand cb = (UIXCommand) clientEvent.getComponent(); 
      cb.broadcast(new ActionEvent(cb)); 
  } 

No comments:

Post a Comment