Beyond JSF Validation using Seam Faces

 

All the above validation tactics can be even enhanced by Seam Faces framework. The purpose of Seam Faces is to provide a fully integrated CDI programming model to the JavaServer Faces (JSF) 2.0 web-framework. With features such as observing Events, providing injection support for life-cycle artifacts (FacesContext, NavigationHandler,) and more.

 

You can learn more about Seam Faces module here.

 

Performing cross-field validation with Seam Faces is absolutely easy using Dependency Injection and <s:validateForm> component in the form you wish to validate. Here's an example of it:


<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:s="http://jboss.org/seam/faces">
 <h:form id="form">
   City:  <h:inputText id="city" value="#{bean.city}" /> <br/>
   State: <h:inputText id="state" value="#{bean.state}" /> <br/>
   Zip:   <h:inputText id="zipCode" value="#{bean.zip}" /> <br/>
  <h:commandButton id="submit" value="Submit" action="#{bean.submit}" />
 
  <s:validateForm validatorId="addressValidator" fields="zip=zipCode" />
 </h:form>
</html>


@FacesValidator("addressValidator")
public class AddressValidator implements Validator
{
 @Inject Directory directory;
 @Inject 
 @InputField String city;
 @Inject 
 @InputField String state;
 @Inject 
 @InputField ZipCode zip;

 

 public void validate(FacesContext context, UIComponent c, Object val) throws ValidatorException {
  if (!directory.exists(city, state, zip))  {
     throw new ValidatorException("Invalid address. Please try again.");
  }
 }
}

In the next tutorial we will see a step-by-step guide for Validating form components using Hibernate Validation.

0
0
0
s2smodern