[jboss-user] [JBoss Seam] - Re: Problems with Validator and Seam when submit form

fabio.ita04 do-not-reply at jboss.com
Wed Dec 27 19:19:40 EST 2006


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.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3996549#3996549

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3996549




More information about the jboss-user mailing list