6 Replies Latest reply on Dec 28, 2006 7:10 AM by cayo

    Problems with Validator and Seam when submit form

      Hi,

      I'm trying to use Seam 1.1 and Hibernate Validation Annotations. My JSF page contains some ajax4j controls (like Seam doc examples) so validation occurs "on the fly". My entity has some constraints (min, max, not null, ..).

      Every constraints works fine with ajax4j (error messages are correctly displayed after onblur, onclick events), but if I submit the form with any constraint violation, no validation occurs (and thus saving inconsistent data, like blank login - that is annotated with @min @max).

      Can anyone help me solve this? (Why validation works at ajax4j events and doesn't at form submission?).

      Thanx
      Fábio.

        • 1. Re: Problems with Validator and Seam when submit form
          ask4saif

          I had the same kind of problem with the hibernate validation annotations, but i was not using a4j in it.

          Is there any kind of exception raised when you submit the form?

          Try to Remove the @NotNull annotaion and then try to run you example, As its alternate use the required="true" in your jsf tags.

          can you mail me your code on ask4saif@gmail.com, i will see what i can do.

          Thank you

          • 2. Re: Problems with Validator and Seam when submit form

            Thanks for the tip! required=true in JSF worked (forms being validated on submission).

            I noted that when *NOT* using required=true, the fields are *ONLY* validated if they *AREN'T EMPTY*.

            So, it's frustrating: when I say @Length(min=5), implicitly I say that property is required, don't?

            Thanx,
            Fábio.

            • 3. Re: Problems with Validator and Seam when submit form

              JSF only validates fields that are submitted. If the field is empty, it isn't submitted and isn't validated. It's just the way that JSF works. (and really the way HTTP works)

              • 4. Re: Problems with Validator and Seam when submit form

                I have a problem which goes in the same direction. I'm interested if anyone had this problem too.
                After validating the form and e.g. have validation messages because of required=true violation on the form, the ajax4jsf actions cannot rerender fields which are currently in validation error.
                It's a problem if the user cannot use the ajax4jsf controls after he tried to validate the form.

                • 5. Re: Problems with Validator and Seam when submit form

                  Cayo,

                  I spent some time with an error like yours. Simple validation (like that in Hibernate Validator) worked fine even after first submission (failed due to required constraints), but complex validation (using some business logic, like verify if a login field is unique) don't. The JSF code:

                  <div class="entry">
                   <s:decorate>
                   <label>#{messages['user.login']}</label>
                   <h:inputText id="login" value="#{user.login}">
                   <a:support event="onblur" reRender="loginError" />
                   </h:inputText>
                   <a:outputPanel id="loginError">
                   <s:message />
                   <c:if test="#{userManager.loginInUse}">
                   #{messages['user.login.in.use']}
                   </c:if>
                   </a:outputPanel>
                   </s:decorate>
                  </div>
                  


                  where #{userManager.loginInUse} is a Session Bean method that was supposed to perform validation (verify if login is unique) at ajax rerendering.

                  The reason of the fails: the backing bean wasn't properly populated, probably because "required" constraints returned page before that. So, a method like user.getLogin (invoked at #{userManager.loginInUse}) returned null, even when the field was typed in the form.

                  So, I decided to move this validation to not be executed at ajax events. Instead, placed it in the action method that is invoked on form submission. If validation fails, add a message in FacesMessages with Fatal Severity, and return.

                  FacesMessages.instance().addFromResourceBundle(
                   FacesMessage.SEVERITY_FATAL, "login.already.in.use");
                  


                  All Fatal errors are displayed as Global Errors at the top of the form:
                  <span>
                   <h:messages infoClass="success" fatalClass="globalErrors" errorStyle="display: none" />
                  </span>
                  


                  This way, business logic validation is executed at the very begining of action method (grant simple validation and proper population of backing bean).

                  Hope it helps.
                  Fábio.

                  • 6. Re: Problems with Validator and Seam when submit form

                    Thanks Fábio!
                    I will try this.