background image

Validating a Component's Value

<< Using the Standard Validators | Using the LongRangeValidator >>
<< Using the Standard Validators | Using the LongRangeValidator >>

Validating a Component's Value

TABLE 11­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.
Similarly to the standard converters, each of these validators has one or more standard error
messages associated with it. If you have registered one of these validators onto a component on
your page, and the validator is not able to validate the component's value, the validator's error
message will display on the page. For example, the error message that displays when the
component's value exceeds the maximum value allowed by LongRangeValidator is the
following:
{1}: Validation Error: Value is greater than allowable maximum of
"{0}"
In this case the {1} substitution parameter is replaced by the component's label or ID, and the
{0}
substitution parameter is replaced with the maximum value allowed by the validator.
See section 2.5.4 of the JavaServer Faces specification for the complete list of error messages. See
"Displaying Error Messages with the message and messages Tags" on page 349
for information
on how to display validation error messages on the page when validation fails.
Validating a Component's Value
In order to validate a component's value using a particular validator, you need to register the
validator on the component. You have three ways to do this:
Nest the validator's corresponding tag (shown in
Table 11­7
) inside the component's tag.
"Using the LongRangeValidator" on page 363
describes how to use the validateLongRange
tag. You can use the other standard tags in the same way.
Refer to a method that performs the validation from the component tag's validator
attribute.
Using the Standard Validators
The Java EE 5 Tutorial · September 2007
362