Search code examples
validationjsfprimefacescommandbutton

CommandButton that checks some required fields but not all of them


I'm trying to do something similar to this:

<h:form id="form">
    <p:inputText id="input1" value="#{mb.input}" required="true" /><br />
    <h:panelGroup id="panelGroup"><br />
        <p:inputText id="input2" required="true" /><br />
        <p:commandButton id="doSomething" value="something" action="#{mb.doSomething}" />
    </h:panelGroup><br />
    <p:commandButton id="save" value="Save" action="#{mb.save}" /><br />
</h:form>

Here is my problem: when I hit the save button I want the whole form to be validated where required="true" (for both input1 and input2, which works fine).

BUT, when I hit the doSomething button I would like it to check only if input2 is filled, ignoring the condition of input1 (in other words: it shouldn't work if input2 is empty but should work even if input1 is). Is there a way to do it? (And I can't use Managed Beans for that!)


Solution

  • use the process attribute of the <p:commandButton> like this

    <p:commandButton id="doSomething" value="something" action="#{mb.doSomething}"  process="input2"/>
    

    that way it will only validate input2 upon click. as per Primefaces Documentation:

    process | null | String | Component(s) to process partially instead of whole view.