Java Reference
In-Depth Information
Recall that the age field is bound to a property of type Integer in our managed
bean. If a user enters a value that is not a valid integer into this field, a validation
error is automatically generated.
Of course, a negative age wouldn't make much sense. However, our application
validates that user input is a valid Integer with essentially no effort on our part.
The email address input field of our page is bound to a property of type String
in our managed bean. As such, there is no built in validation to make sure that the
user enters a valid email address. In cases like this, we need to write our own custom
JSF validator.
Custom JSF validators must implement the javax.faces.validator.Validator
interface. This interface contains a single method named validate() , this method
takes three parameters, an instance of javax.faces.context.FacesContext , an
instance of javax.faces.component.UIComponent containing the JSF component
we are validating, and an instance of java.lang.Object containing the user-entered
value for the component. The following example illustrates a typical custom validator:
package com.ensode.jsf.validators;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
 
Search WWH ::




Custom Search