Al2x
Al2x

Reputation: 1039

Validate only specific parts of the form instead of whole form

I have a whole form with a lot of components including a p:tab

When I click on p:commandButton id=c1 to submit the whole form content:

What is the best solution for this ? Thanks in advance.

Upvotes: 3

Views: 3343

Answers (2)

kbeck
kbeck

Reputation: 31

FYI you can also process the id of a panel containing the controls you want to validate - example :

<p:outputPanel id="thisPanel">
    <p:inputText id="field1" ... required="#{not empty param[c1.clientId]}" />
    <p:inputText id="field2" ... required="#{not empty param[c1.clientId]}" />

    <p:commandButton id="c2" ... process="thisPanel" />

Upvotes: -1

BalusC
BalusC

Reputation: 1108567

You seem to be using the "God Form" anti-pattern. Everything is thrown together in a single <h:form>. This is a poor design/practice. Most sensible way would be to put the fields and buttons in separate forms so that only the related fields and buttons are in its own form so that the form submit doesn't unnecessarily submit/process/convert/validate irrelvant data in other forms.

See also:


If that's not possible due to some (strange?) design restrictions, then there are at least 2 other ways:

  1. If you're using ajax, then you can make use of the process attribute. It defaults to @form which would process the entire form. It accepts a space separated string of (relative) client IDs of input field which you'd like to process during submit.

    <p:inputText id="field1" ... required="true" />
    <p:inputText id="field2" ... required="true" />
    ...
    <p:inputText id="field3" ... required="true" />
    <p:inputText id="field4" ... required="true" />
    ...
    <p:commandButton id="c1" ... process="field1 field2" />
    ...
    <p:commandButton id="c2" ... process="field3 field4" />
    

    See also: Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes

  2. If you're not using ajax, or desire a non-ajax fallback, then just check in the required attribute which button has been pressed. This is easy by checking the presence of the button's client ID in the request parameter map.

    <p:inputText id="field1" ... required="#{not empty param[c1.clientId]}" />
    <p:inputText id="field2" ... required="#{not empty param[c1.clientId]}" />
    ...
    <p:inputText id="field3" ... required="#{not empty param[c2.clientId]}" />
    <p:inputText id="field4" ... required="#{not empty param[c2.clientId]}" />
    ...
    <p:commandButton id="c1" binding="#{c1}" ... />
    ...
    <p:commandButton id="c2" binding="#{c2}" ... />
    

    (note: no additional bean properties necessary for c1 or c2! the code is as-is)

    See also How to let validation depend on the pressed button?

    You can refactor this somewhat with a more self-documenting variable name:

    <c:set var="c1ButtonPressed" value="#{not empty param[c1.clientId]}" />
    <c:set var="c2ButtonPressed" value="#{not empty param[c2.clientId]}" />
    ...
    <p:inputText id="field1" ... required="#{c1ButtonPressed}" />
    <p:inputText id="field2" ... required="#{c1ButtonPressed}" />
    ...
    <p:inputText id="field3" ... required="#{c2ButtonPressed}" />
    <p:inputText id="field4" ... required="#{c2ButtonPressed}" />
    ...
    <p:commandButton id="c1" binding="#{c1}" ... />
    ...
    <p:commandButton id="c2" binding="#{c2}" ... />
    

Upvotes: 8

Related Questions