Using the Standard Validators (original) (raw)
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 11-6 lists all the standard validator classes and the tags that allow you to use the validators from the page.
Table 11-6 The Validator Classes
Validator Class | Tag | Function |
---|---|---|
BeanValidator | validateBean | Registers a bean validator for the component. |
BeanValidator | validateWholeBean | Allows cross-field validation by enabling class-level bean validation on CDI-based backing beans. |
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 ajava.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. |
RegexValidator | validateRegex | Checks whether the local value of a component is a match against a regular expression from thejava.util.regex package. |
RequiredValidator | validateRequired | Ensures that the local value is not empty on an EditableValueHolder component. |
All of 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.
Similar 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 unable 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 byLongRangeValidator
is as follows:
{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.
Instead of using the standard validators, you can use Bean Validation to validate data. If you specify bean validation constraints on your managed bean properties, the constraints are automatically placed on the corresponding fields on your applications web pages. SeeChapter 23, "Introduction to Bean Validation" for more information. You do not need to specify thevalidateBean
tag to use Bean Validation, but the tag allows you to use more advanced Bean Validation features. For example, you can use thevalidationGroups
attribute of the tag to specify constraint groups.
You can also create and register custom validators, although Bean Validation has made this feature less useful. For details, seeCreating and Using a Custom Validator.
Validating a Component’s Value
To validate a component’s value using a particular validator, you need to register that validator on the component. You can do this in one of the following ways.
- Nest the validator’s corresponding tag (shown in Table 11-6) inside the component’s tag. Using Validator Tagsexplains 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. - Nest a validator tag inside the component tag, and use either the validator tag’s
validatorId
attribute or itsbinding
attribute to refer to the validator.
The validatorId
attribute works similarly to the converterId
attribute of the converter
tag, as described inConverting a Component’s Value.
Keep in mind that validation can be performed only on components that implement EditableValueHolder
, because these components accept values that can be validated.
Using Validator Tags
The following example shows how to use the f:validateLongRange
validator tag on an input component named quantity
:
<h:inputText id="quantity" size="4" value="#{item.quantity}">
<f:validateLongRange minimum="1"/>
</h:inputText>
<h:message for="quantity"/>
This tag requires the user to enter a number that is at least 1. ThevalidateLongRange
tag also has a maximum
attribute, which sets a maximum value for the input.
The attributes of all the standard validator tags accept EL value expressions. This means that the attributes can reference managed bean properties rather than specify literal values. For example, thef:validateLongRange
tag in the preceding example can reference managed bean properties called minimum
and maximum
to get the minimum and maximum values acceptable to the validator implementation, as shown in this snippet from the guessnumber-jsf
example:
<h:inputText id="userNo"
title="Type a number from 0 to 10:"
value="#{userNumberBean.userNumber}">
<f:validateLongRange minimum="#{userNumberBean.minimum}"
maximum="#{userNumberBean.maximum}"/>
</h:inputText>
The following f:validateRegex
tag shows how you might ensure that a password is from 4 to 10 characters long and contains at least one digit, at least one lowercase letter, and at least one uppercase letter:
<f:validateRegex pattern="((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,10})"
for="passwordVal"/>