Using a standard validator

Using the standard JSF validators can be a simple task if you keep in mind two simple observations:

  • They can be specified using a component's validator attribute or by nesting JSF-provided tags
  • Validation can be performed only on UIInput components or components whose classes extend UIInput

In this recipe, you will see how to use the JSF standard validators listed next:

  • LengthValidator: Counts the number of characters of a value and checks if it fits in a given range. The range boundaries are given by two attributes, as follows:
    • minimum: The minimum acceptable number of characters
    • maximum: The maximum acceptable number of characters
  • LongRangeValidator: Attempts to convert the value to a number of Java long primitive type and checks to see if that number fits in a given range. The range boundaries are given by two attributes, as follows:
    • minimum: The minimum acceptable number
    • maximum: The maximum acceptable number

If these attributes are skipped, then the validator only checks if the value is numeric.

  • DoubleRangeValidator: Attempts to convert the value to a number of Java double primitive type and checks to see if that number fits in a given range. The range boundaries are given by two attributes, as follows:
    • minimum: The minimum acceptable number
    • maximum: The maximum acceptable number

If these attributes are skipped, then the validator will only check if the value is numeric.

In this recipe, you will see how to use the first two previous validators.

Getting ready

We developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.

How to do it...

Let's suppose that we have a form with two fields representing a user's age and name. The age should be between 18 and 50 (we will apply the LongRangeValidator) and the name length will be between 5 and 25 characters (we will use the LengthValidator). Now, the corresponding form will look like this:

<h:form id="UserForm">
<h:outputText value="Insert your age:"/><br />
<h:inputText id="userAgeID" required="true"
value="#{userBean.userAge}">
<f:validateLongRange minimum="18" maximum="50"/>
</h:inputText>
<h:message showSummary="true" showDetail="false" for="userAgeID"
style="color: red; text-decoration:overline"/>
<br />
<h:outputText value="Insert your first name:"/><br />
<h:inputText id="userNameID" required="true"
value="#{userBean.firstName}">
<f:validateLength minimum="5" maximum="25" />
</h:inputText>
<h:message showSummary="true" showDetail="false" for="userNameID"
style="color: red; text-decoration:overline"/>
<br />
<h:commandButton id="submit" action="response?faces-redirect=true"
value="Submit"/>
</h:form>

How it works...

The mechanism is simple! Before populating the managed bean, the values are validated by the specified validators. If an error occurs while validating the values then the process returns an error message and displays the form again.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and it is named: Using_a_standard_validator.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset