How to let validation depend on the pressed button?

I understand that you want to filter based on the name input field. The <p:commandButton> sends by default an ajax request and has a process attribute wherein you can specify which components you’d like to process during the submit. In your particular case, you should then process only the name input field and the current button (so that its action will be invoked).

<p:commandButton process="@this name" ... />

The process attribute can take a space separated collection of (relative) client IDs of the components, wherein @this refers to the current component. It defaults in case of <p:commandButton> to @form (which covers all input fields of the current form and the pressed button), that’s why they were all been validated in your initial attempt. In the above example, all other input fields won’t be processed (and thus also not validated).

If you however intend to skip the required validation for all fields whenever the button in question is been pressed, so that you can eventually process multiple fields which doesn’t necessarily need to be all filled in, then you need to make the required="true" a conditional instead which checks if the button is been pressed or not. For example, let it evaluate true only when the save button has been pressed:

<p:inputText ... required="#{not empty param[save.clientId]}" />
...
<p:inputText ... required="#{not empty param[save.clientId]}" />
...
<p:commandButton binding="#{save}" value="Save" ... />

This way it won’t be validated as required="true" when a different button is pressed. The trick in the above example is that the name of the pressed button (which is essentially the client ID) is been sent as request parameter and that you could just check its presence in the request parameter map.

See also:

Leave a Comment