1 Reply Latest reply on Oct 20, 2009 1:16 PM by Nikos Paraskevopoulos

    Field level validation on a AJAX onChange event

    Evelyn Jeba Newbie
      I am having two fields, Country and State. Depending on the country selected, the state field is populated, validated and rendered using ajax support. But the state field must not be validated at this level, it must only be populated and rendered. Only when the persist method is called during 'Save', it must validate all the fields. What should be done in order to achieve this?

      The existing piece of code given below, actually validates the state field on re-population.

      <s:decorate id="countryField" template="/layout/edit.xhtml">
          <ui:define name="label">Country</ui:define>
              <h:selectOneMenu id="country" required="true" value="#{address.country}"
                                               valueChangeListener="#{addressHome.countryChanged}">
                  <s:selectItems value="#{countryList.resultList}" var="country" label="#{country.name}"
                                           noSelectionLabel="Please Select" hideNoSelectionLabel="true"/>
                  <a:support event="onchange" reRender="stateField"/>
                  <s:convertEntity/>
              </h:selectOneMenu>
      </s:decorate>

      <s:decorate id="stateField" template="/layout/edit.xhtml">
          <ui:define name="label">State</ui:define>
          <h:selectOneMenu id="state" required="true" value="#{address.state}">
              <s:selectItems value="#{addressHome.instance.country.states}" var="state"                  
                      label="#{state.name}" noSelectionLabel="Please Select" hideNoSelectionLabel="true"/>
              <s:convertEntity/>
          </h:selectOneMenu>
      </s:decorate>

      AddressHome.java

          public void countryChanged(ValueChangeEvent event) {
              if (event.getNewValue() != null) {
                  getInstance().setCountry((Country)event.getNewValue());
              }

              FacesContext.getCurrentInstance().renderResponse();
          }
        • 1. Re: Field level validation on a AJAX onChange event
          Nikos Paraskevopoulos Novice

          Try:


          <a:support event="onchange" reRender="stateField" ajaxSingle="true" />
          



          and remove the valueChangeListener="#{addressHome.countryChanged}". The method setCountry() will be called normally in the update model phase of the AJAX request, because it is the value of <h:selectOneMenu id="country" value="#{address.country}">, as long as <s:convertEntity/> is properly set up.


          Perhaps this is a question for the RichFaces forum though...