background image

Using the LongRangeValidator

<< Validating a Component's Value | Binding Component Values and Instances >>
<< Validating a Component's Value | Binding Component Values and Instances >>

Using the LongRangeValidator

Nest a validator tag inside the component tag and use either the validator tag's
validatorId
attribute or its binding attribute to refer to the validator.
See
"Referencing a Method That Performs Validation" on page 372"Referencing a Method That
Performs Validation" on page 372
for more information on using the validator attribute.
The validatorId attribute works similarly to the converterId attribute of the converter tag,
as described in
"Converting a Component's Value" on page 355
. See
"Binding Converters,
Listeners, and Validators to Backing Bean Properties" on page 369
for more information on
using the binding attribute of the validator tag.
Keep in mind that validation can be performed only on components that implement
EditableValueHolder
because these components accept values that can be validated.
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.
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
called minimum to get the minimum value acceptable to the validator implementation:
<f:validateLongRange minimum=
"#{ShowCartBean.minimum}" />
Using the Standard Validators
Chapter 11 · Using JavaServer Faces Technology in JSP Pages
363