3 Replies Latest reply on Jun 12, 2007 5:45 AM by Blondel Michaël

    ajax4jsf trouble after a form has failed JSF validation

    Alex Walker Newbie

      I'm having a problem getting ajax4jsf to play nice with jsf form validation. The page below uses ajax4jsf to run a method from the bean (session scope) when the value in the "state" list changes and update a couple of fields. This works great under most circumstances. However, if the user submits the form *without* filling in the required "addressName" field, the form does not submit and the error message is displayed. This is as it should be. The trouble comes when, if the form has been submitted and is displaying the error message for the "addressName" field, the user changes the "state" field, which should still fire off the "loadState" method. In this case, the "loadState" method never fires and none of the fields in the "reRender" list are refreshed, even if there is now a value in the "addressname" field.

      I thought that bypassing validation by adding an immediate="true" to the onload event attached to the "state" field would do the trick. When I put that in the "loadState" method did fire, but none of the fields in the "rerender" list updated. This still seems like it could be the way to go, but I just can't make it work.

      The snippet below should show all of the relvant code, any help would be much appreciated.

      Thanks,
      Alex

      
      <s:subForm id = "locationSubForm">
      <h:panelGroup id="locationGroup">
       <h:panelGrid columns="2" style="margin-top:10px;" id="locationGrid" border="0" cellpadding="2">
      
       <h:outputText value="Address Name:" />
       <h:inputText id="addressName" value="#{locationForm.addressName}" required="true"/>
      
       <h:message for="addressName" />
       <h:outputText value="" />
      
       <h:outputText value="Street Address:" />
       <h:inputText id="streetAddress" value="#{locationForm.streetAddress}" />
      
       <h:outputText value="City:" />
       <h:inputText id="city" value="#{locationForm.city}" />
      
       <h:outputText value="State:" />
      
       <h:selectOneMenu value="#{locationForm.state}" style="padding:0px;margin:0px;">
       <a4j:support event="onchange" action="#{locationForm.loadState}" reRender="latitude, longitude, city, zipcode" ajaxSingle="true"/>
       <f:selectItems value ="#{locationForm.stateList}" />
       </h:selectOneMenu>
      
       ...
      
       <t:commandButton action="#{locationForm.submitNewLocation}" value="Submit" actionFor="locationSubForm"/>
       <h:outputText value=" " escape="false"/>
       </h:panelGrid>
       </h:panelGroup>
      </s:subForm>
      


        • 1. Re: ajax4jsf trouble after a form has failed JSF validation
          Sergey Smirnov Master

          What do you have in your run-time? I see s:subForm. Where it comes from?

          • 2. Re: ajax4jsf trouble after a form has failed JSF validation
            Alex Walker Newbie

            It did occur to me that the subform might be causing problems. That doesn't seem to be the case thought, as a stripped-down test page with a single form still had the same error. Here's what I'm testing with:

            <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
            <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
            <%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
            <%@ taglib uri="https://ajax4jsf.dev.java.net/ajax" prefix="a4j"%>
            <%@ taglib uri="http://myfaces.apache.org/sandbox" prefix="s" %>
            
            <f:view>
            <html>
            <body>
            <h:form id = "locationForm">
            <h:panelGroup id="locationGroup">
             <h:panelGrid columns="2" style="margin-top:10px;" id="locationGrid" border="0" cellpadding="2">
            
             <h:outputText value="Address Name:" />
             <h:inputText id="addressName" value="#{locationForm.addressName}" required="true"/>
            
             <h:outputText value="" />
             <h:message for="addressName" errorClass="errorDisplay"/>
            
             <h:outputText value="City:" />
             <h:inputText id="city" value="#{locationForm.city}" />
            
             <h:outputText value="State:" />
             <h:selectOneMenu value="#{locationForm.state}" style="padding:0px;margin:0px;" >
             <a4j:support event="onchange" action="#{locationForm.loadState}" reRender="zipcode, latitude, longitude, city" ajaxSingle="true" immediate="false"/>
             <f:selectItems value ="#{locationForm.stateList}" />
             </h:selectOneMenu>
            
             <h:outputText value="Zip Code:" />
             <h:selectOneMenu value="#{locationForm.zipcode}" style="padding:0px;margin:0px;" id="zipcode" >
             <a4j:support event="onchange" action="#{locationForm.loadZip}" reRender="latitude, longitude, city" ajaxSingle="true" immediate="false">
             </a4j:support>
             <f:selectItems value ="#{locationForm.zipList}" />
             </h:selectOneMenu>
            
             <h:outputText value="Latitude:" />
             <h:inputText id="latitude" value="#{locationForm.latitude}">
             <t:validateRegExpr pattern="-?\d+\.?\d*" message="Error: The longitutde field must be numeric"></t:validateRegExpr>
             </h:inputText>
            
             <h:outputText value="" />
             <a4j:outputPanel ajaxRendered="true">
             <t:message for="latitude" errorClass="errorDisplay" />
             </a4j:outputPanel>
            
             <h:outputText value="Longitude:" />
             <h:inputText id="longitude" value="#{locationForm.longitude}" >
             <t:validateRegExpr pattern="-?\d+\.?\d*" message="Error: The longitutde field must be numeric"></t:validateRegExpr>
             </h:inputText>
            
             <h:outputText value="" />
             <a4j:outputPanel ajaxRendered="true">
             <t:message for="longitude" errorClass="errorDisplay" />
             </a4j:outputPanel>
            
             <t:commandButton action="#{locationForm.submitNewLocation}" value="Submit" actionFor="locationSubForm"/>
             <h:outputText value=" " escape="false"/>
            
             </h:panelGrid>
             </h:panelGroup>
             </h:form>
            </body>
            </html>
            </f:view>
            


            • 3. Re: ajax4jsf trouble after a form has failed JSF validation
              Blondel Michaël Newbie

              Have you find the problem ?
              I have a similar prb with the component suggestionbox (richfaces).
              If a fiel required is not set, the JSF validation fail and then the component doesn't work anymore.
              I think it's the same prob.
              Thanks for your solve