I have an IceFaces form with several HtmlCommandButtons
on it. I have some input fields that have attached validators. The validation works well, but runs and blocks the process even if I press buttons different than the submit one. Logical because all my buttons are basically the same. The question is: how to distinguish between the buttons from the aspect of validation?
I cannot provide XHTML snippet because my form is build dynamically from Java code. The buttons are created this way:
HtmlCommandButton comp = new HtmlCommandButton();
comp.setId("btn" + StringUtil.toId(label) + "_" + action);
comp.setTitle(label);
comp.setValue(label);
comp.setStyleClass("commandexbutton commandexbutton-" + StringUtil.toId(label));
comp.addActionListener(JSFBuilderHelper.createActionListener(getActionListenerStr()));
comp.setPartialSubmit(true);
At simplest, you could set the UICommand
component's immediate
attribute to true
. It will then skip the processing of UIInput
components which does not have this attribute to true
.
comp.setImmediate(true);
For a detailed explanation of the usage of this attribute, see the 2nd half of this answer.
If that's not an option, then your best bet is to put the cancel button in a separate form so that it effectively ends up like:
<h:form>
input fields
submit button
</h:form>
<h:form>
cancel button
</h:form>
If that's also not an option due to design/layout restrictions (read: not fixable by just CSS/JS), then you basically need to check in every validator which button is been pressed. You could check that by the presence of the button's client name (ID) in the request parameter map. For example, as component's attribute:
required="#{not empty param['formId:submitButtonId']}"
or
required="#{empty param['formId:cancelButtonId']}"
or in the beginning of the validator's validate()
method:
if (externalContext.getRequestParameterMap().get("formId:submitButtonId") == null) {
return; // Skip validation when normal submit button is not pressed.
}
or
if (externalContext.getRequestParameterMap().get("formId:cancelButtonId") != null) {
return; // Skip validation when cancel button is pressed.
}