Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

Using the Standard Validators

JavaServer Faces technology provides a set of standard classes and associated tags that page authors and application developers can use to validate a component's data. Table 18-7 lists all the standard validator classes and the tags that allow you to use the validators from the page.

Table 18-7 The Validator Classes 
Validator Class
Tag
Function
DoubleRangeValidator
validateDoubleRange
Checks whether the local value of a component is within a certain range. The value must be floating-point or convertible to floating-point.
LengthValidator
validateLength
Checks whether the length of a component's local value is within a certain range. The value must be a java.lang.String.
LongRangeValidator
validateLongRange
Checks whether the local value of a component is within a certain range. The value must be any numeric type or String that can be converted to a long.

All these validator classes implement the Validator interface. Component writers and application developers can also implement this interface to define their own set of constraints for a component's value.

When using the standard Validator implementations, you don't need to write any code to perform validation. You simply nest the standard validator tag of your choice inside a tag that represents a component of type UIInput (or a subclass of UIInput) and provide the necessary constraints, if the tag requires it. Validation can be performed only on UIInput components or components whose classes extend UIInput because these components accept values that can be validated.

This section shows you how to use the standard Validator implementations.

See The UIMessage and UIMessages Components for information on how to display validation error messages on the page.

Requiring a Value

The name inputText tag on the bookcashier.jsp page has a required attribute, which is set to true. Because of this, the JavaServer Faces implementation checks whether the value of the component is null or is an empty String.

If your component must have a non-null value or a String value at least one character in length, you should add a required attribute to your component tag and set it to true. If your tag does have a required attribute that is set to true and the value is null or a zero-length string, no other validators registered on the tag are called. If your tag does not have a required attribute set to true, other validators registered on the tag are called, but those validators must handle the possibility of a null or zero-length string.

Here is the name inputText tag:

<h:inputText id="name" size="50" 
  value="#{cashier.name}" required="true"> 
  ...
</h:inputText> 

Using the LongRangeValidator

The Duke's Bookstore application uses a validateLongRange tag on the quantity input field of the bookshowcart.jsp page:

<h:inputText id="quantity"   size="4" 
  value="#{item.quantity}" >
  <f:validateLongRange minimum="1"/>
</h:inputText>
<h:message for="quantity"/> 

This tag requires that the user enter a number that is at least 1. The size attribute specifies that the number can have no more than four digits. The validateLongRange tag also has a maximum attribute, with which you can set a maximum value of the input.

Divider
Download
FAQ
History
PrevHomeNext API
Search
Feedback
Divider

All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.