1

I have a p:ajax within a form which triggers on change event in a select. Also I have a separate submit button to submit the form.

The form contains multiple validations. I want to bypass all the validations during the p:ajax call. Instead all the validations should happen during form submission.

This is my form: Field1 and Field2 are mandatory. But these validations should be bypassed during p:ajax call which in turn renders Field3 based on the selected Field2 value.

How can I do this?

<h:body>

    <h:form id="formId">

        <p:outputPanel autoUpdate="true">

            <p:message for="field1Id field2Id" />

            <p:panel id="panelId">
                <p:panelGrid>

                    <p:row>
                        <p:column>
                            <h:outputText value="Field1:" />
                        </p:column>
                        <p:column>
                            <p:inputText
                                id="field1Id"
                                required="true"
                                requiredMessage="Field1 is Required!"
                                value="#{testBean.field1}"
                                size="5"
                                maxlength="30" />
                        </p:column>
                    </p:row>

                    <p:row>
                        <p:column>
                            <h:outputText value="Field2:" />
                        </p:column>
                        <p:column>
                            <p:selectOneMenu
                                id="field2Id"
                                value="#{testBean.field2}"
                                required="true"
                                requiredMessage="Field2 is Required!">
                                <f:selectItems
                                    value="#{testBean.fields}"
                                    var="var1"
                                    itemDescription="#{var1.description}"
                                    itemLabel="#{var1.description}"
                                    itemValue="#{var1.id}" />

                                <p:ajax
                                    process="@form"
                                    event="change" />

                            </p:selectOneMenu>
                        </p:column>
                    </p:row>

                    <p:row rendered="#{testBean.field2 > 0}">
                        <p:column>
                            <h:outputText value="Field3:" />
                        </p:column>
                        <p:column>
                            <p:inputText
                                value="#{testBean.field3}"
                                size="5"
                                maxlength="10" />
                        </p:column>
                    </p:row>

                </p:panelGrid>
                <p:commandButton
                    value="Save"
                    action="#{testBean.saveForm()}"
                    process="@form" />
            </p:panel>
        </p:outputPanel>

    </h:form>

</h:body>
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
Ani
  • 722
  • 9
  • 27

2 Answers2

3

You can achieve by below code. My code just add binding attribute(binding="{save}") at the button, and also change required="true" to required="#{not empty param[save.clientId]}"

<h:body>
    <h:form id="formId">
        <p:outputPanel autoUpdate="true">
            <p:message for="field1Id field2Id" />
            <p:panel id="panelId">
                <p:panelGrid>
                    <p:row>
                        <p:column>
                            <h:outputText value="Field1:" />
                        </p:column>
                        <p:column>
                            <p:inputText
                                id="field1Id"
                                required="#{not empty param[save.clientId]}"
                                requiredMessage="Field1 is Required!"
                                value="#{testBean.field1}"
                                size="5"
                                maxlength="30" />
                        </p:column>
                    </p:row>
                    <p:row>
                        <p:column>
                            <h:outputText value="Field2:" />
                        </p:column>
                        <p:column>
                            <p:selectOneMenu
                                id="field2Id"
                                value="#{testBean.field2}"
                                required="#{not empty param[save.clientId]}"
                                requiredMessage="Field2 is Required!">
                                <f:selectItems
                                    value="#{testBean.fields}"
                                    var="var1"
                                    itemDescription="#{var1.description}"
                                    itemLabel="#{var1.description}"
                                    itemValue="#{var1.id}" />
                                <p:ajax
                                    process="@form"
                                    event="change" />

                            </p:selectOneMenu>
                        </p:column>
                    </p:row>
                    <p:row rendered="#{testBean.field2 > 0}">
                        <p:column>
                            <h:outputText value="Field3:" />
                        </p:column>
                        <p:column>
                            <p:inputText
                                value="#{testBean.field3}"
                                size="5"
                                maxlength="10" />
                        </p:column>
                    </p:row>
                </p:panelGrid>
                <p:commandButton
                    value="Save"
                    binding="{save}"
                    action="#{testBean.saveForm()}"
                    process="@form" />
            </p:panel>
        </p:outputPanel>
    </h:form>
</h:body>

This approach is proposed by this link.

Community
  • 1
  • 1
wittakarn
  • 3,124
  • 1
  • 18
  • 31
1

If you're willing to use omnifaces, there is an ignoreValidationFailed tag.

Check the example at omnifaces showcase:

<o:form>
  ...
  <h:commandButton value="save valid data" action="#{bean.saveValidData}">
    <o:ignoreValidationFailed />
    <f:ajax execute="@form" />
  </h:commandButton>

</o:form>
nuno
  • 1,771
  • 1
  • 19
  • 48