10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using the StandardValidators<br />

360<br />

TABLE 11–7 <strong>The</strong> Validator Classes<br />

Validator Class Tag Function<br />

DoubleRangeValidator validateDoubleRange Checks whether the local value of a component is within<br />

a certain range. <strong>The</strong> value must be floating-point or<br />

convertible to floating-point.<br />

LengthValidator validateLength Checks whether the length of a component’s local value<br />

is within a certain range. <strong>The</strong> value must be a<br />

java.lang.String.<br />

LongRangeValidator validateLongRange Checks whether the local value of a component is within<br />

a certain range. <strong>The</strong> value must be any numeric type or<br />

String that can be converted to a long.<br />

All these validator classes implement the Validator interface. Component writers and<br />

application developers can also implement this interface to define their own set of constraints<br />

for a component’s value.<br />

Similarly to the standard converters, each of these validators has one or more standard error<br />

messages associated with it. If you have registered one of these validators onto a component on<br />

your page, and the validator is not able to validate the component’s value, the validator’s error<br />

message will display on the page. For example, the error message that displays when the<br />

component’s value exceeds the maximum value allowed by LongRangeValidator is the<br />

following:<br />

{1}: Validation Error: Value is greater than allowable maximum of "{0}"<br />

In this case the {1} substitution parameter is replaced by the component’s label or ID, and the<br />

{0} substitution parameter is replaced with the maximum value allowed by the validator.<br />

See section 2.5.4 of the <strong>Java</strong>Server Faces specification for the complete list of error messages. See<br />

“Displaying Error Messages with the message and messages Tags” on page 347 for information<br />

on how to display validation error messages on the page when validation fails.<br />

Validating a Component’sValue<br />

In order to validate a component’s value using a particular validator, you need to register the<br />

validator on the component. You have three ways to do this:<br />

■ Nest the validator’s corresponding tag (shown in Table 11–7) inside the component’s tag.<br />

“Using the LongRangeValidator” on page 361 describes how to use the validateLongRange<br />

tag. You can use the other standard tags in the same way.<br />

■ Refer to a method that performs the validation from the component tag’s validator<br />

attribute.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!