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 10-7 lists all the standard validator classes and the tags that allow you to use the validators from the page.
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 you want to register a standard validator on a component, you just nest the standard validator's tag inside the tag representing a component that implements
EditableValueHolder
and provide the necessary constraints, if the validator tag requires it. Validation can be performed only on components that implementEditableValueHolder
because these components accept values that can be validated.Each standard validator supports a binding attribute, which is used to bind a validator implementation to a backing bean property, similarly to the way binding attributes are used with converter and listener tags, as described previously in this chapter.
Similarly to the standard converters, each of these validators has a 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:
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.
This section shows you how to use the
LongRangeValidator
implementation. The other validators work in a similar way.See The UIMessage and UIMessages Components for information on how to display validation error messages on the page when validation fails.
Requiring a Value
The
name
inputText
tag on thebookcashier.jsp
page has arequired
attribute, which is set totrue
. 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 aString
value at least one character in length, you should add arequired
attribute to your component tag and set it totrue
. If your tag does have arequired
attribute that is set totrue
and the value isnull
or a zero-length string, no other validators registered on the tag are called. If your tag does not have arequired
attribute set totrue
, other validators registered on the tag are called, but those validators must handle the possibility of anull
or zero-length string.Here is the
name
inputText
tag:Using the LongRangeValidator
The Duke's Bookstore application uses a
validateLongRange
tag on thequantity
input field of thebookshowcart.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. ThevalidateLongRange
tag also has amaximum
attribute, with which you can set a maximum value of the input.The attributes of all the standard validator tags accept value expressions. This means that the attributes can reference backing bean properties rather than specify literal values. For example, the
validateLongRange
tag in the preceding example can reference a backing bean property calledminimum
to get the minimum value acceptable to the validator implementation: