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
490 views
in Technique[技术] by (71.8m points)

input - How to skip validation on certain fields in JSF 2 when required

I have two p:dialog. Each one contains some input fields which are marked as required. Only one dialog is shown at a time but when I submit on any of the dialog, JSF also validates the input fields of the dialog which is not shown. What is the best approach to skip the validations in JSF 2 for the dialog which is not shown. One approach could be that I set the required="condation". But dont know what that condition could be and is it goin to work.

Each dialog is initially hidden. Each one has its own button to show. When one is active and I click the save button there is a validation error even when the required field has values. The validation error comes from the inactive dialog panel. May be it give some idea what i am trying to do.

UPDATE

<p:dialog header="Edit Group" widgetVar="dialog_groupEdit" resizable="false"  
          width="300" showEffect="bounce" hideEffect="explode" modal="true" position="center">
    <h:panelGrid id="panel_groupEdit" columns="2">
        <h:outputText value="Group Name: "/>
        <h:inputText id="input_gnameEdit" size="26" value="#{groupBean.selectionGroup.gname}" required="true" requiredMessage="Edit Group: Name is required."/>
        <h:outputText value="Description:"/>
        <h:inputTextarea rows="6" cols="23" value="#{groupBean.selectionGroup.description}"/>
        <div></div>
        <p:commandButton value="Save" action="#{groupBean.saveGroupChanges}" oncomplete="#{args.validationFailed ? '':'dialog_groupEdit.hide()'}"
                                    update="panel_groups panel_groupEdit" style="float:right;"/>
        <div></div>
        <p:message for="input_gnameEdit"/>
    </h:panelGrid>
    </p:dialog>

    <p:dialog header="New Group" widgetVar="dialog_groupCreate" resizable="false"  
          width="300" showEffect="bounce" hideEffect="explode" modal="true" position="center">
    <h:panelGrid id="panel_groupCreate" columns="2">
        <h:outputText value="Group Name: "/>
        <h:inputText id="input_gnameCreate" size="26" value="#{groupBean.createGroup.gname}" required="true" requiredMessage="New Group: Name is reqired."/>
        <h:outputText value="Description:"/>
        <h:inputTextarea rows="6" cols="23" value="#{groupBean.createGroup.description}"/>
        <div></div>
        <p:commandButton value="Save" actionListener="#{groupBean.saveGroupCreate}" oncomplete="#{empty groupBean.createGroup.gname ? ' ':'dialog_groupCreate.hide()'}"
                                    update="panel_groupCreate panel_groups" style="float:right;"/>
        <div></div>
        <p:message for="input_gnameCreate"/>
    </h:panelGrid>
    </p:dialog>
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

One approach could be that I set the required="condation".

This is going to be clumsy. Just put each individual form in its own separate <h:form> component, or use the process attribute of the <p:commandButton> to identify the region you'd like to process.

<p:commandButton process="panel_groupEdit" ... />

...

<p:commandButton process="panel_groupCreate" ... />

Having a "God" form is in any way a poor practice.

See also:


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

...