How to skip validation on certain fields in JSF 2

2020-02-07 05:32发布

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>

2条回答
ら.Afraid
2楼-- · 2020-02-07 06:14

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:

查看更多
Animai°情兽
3楼-- · 2020-02-07 06:25

When you are using <p:dialog some of the good practices as advised on the PF forum are -

  1. Never put your <p:dialog inside any other component. Place it somewhere so that it falls right under the <h:body.
  2. Give every <p:dialog it's own form. But, don't place the dialog inside the form, place the form inside the dialog. It's not good to update the dialog itself, only it's content. (I think this should solve your issue also.)

e.g.

<p:dialog widgetVar="dlg" ...
    <h:form ...
        <h:panelGroup id="dlgContent" ... 
查看更多
登录 后发表回答