Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

validation - 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:

  • I need to validate the whole form required messages but I do need to ignore the p:tab required messages fields.
  • If I click on p:commandButton id=c2 inside the p:tab, I need to validate only the required messages fields inside the p:tab.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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}" ... />
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...