Java Reference
In-Depth Information
If you choose to implement the Validator interface and you want to allow the page au-
thor to configure the validator's attributes from the page, you also must specify a custom
tag for registering the validator on a component.
If you prefer to configure the attributes in the Validator implementation, you can forgo
specifying a custom tag and instead let the page author register the validator on a compon-
ent using the f:validator tag, as described in Using a Custom Validator ” on page
132 .
You can also create a managed bean property that accepts and returns the Validator
implementation you create, as described in “Writing Properties Bound to Converters,
Listeners, or Validators” in The Java EE 6 Tutorial: Basic Concepts . You can use the
f:validator tag's binding attribute to bind the Validator implementation to the
managed bean property.
Usually, you will want to display an error message when data fails validation. You need to
store these error messages in a resource bundle.
After creating the resource bundle, you have two ways to make the messages available to
the application. You can queue the error messages onto the FacesContext program-
matically, or you can register the error messages in the application configuration resource
file, as explained in “ Registering Application Messages on page 155 .
For example, an e-commerce application might use a general-purpose custom validator
called FormatValidator.java to validate input data against a format pattern that
is specified in the custom validator tag. This validator would be used with a Credit Card
Number field on a Facelets page. Here is the custom validator tag:
Click here to view code image
<mystore:formatValidator
formatPatterns="9999999999999999|9999 9999 9999
9999|9999-9999-9999-9999"/>
According to this validator, the data entered in the field must be one of the following:
• A 16-digit number with no spaces
• A 16-digit number with a space between every four digits
• A 16-digit number with hyphens between every four digits
The f:validateRegex tag makes a custom validator unnecessary in this situation.
However, the rest of this section describes how this validator would be implemented and
how to specify a custom tag so that the page author could register the validator on a com-
ponent.
Search WWH ::




Custom Search