The Apache MyFaces Commons project contains a set of validators (myfaces-validators
), converters (myfaces-converters
), and utils (myfaces-commons-utils
). These are JARs that can be used with any JSF framework.
In this recipe, we are using validators. The most widely used validators are:
<mcv:validateCSV>
<mcv:validateCompareTo>
<mcv:validateCreditCard>
<mcv:validateDateRestriction>
<mcv:validateEmail>
<mcv:validateISBN>
<mcv:validateRegExpr>
<mcv:validateUrl>
In this recipe you will see how to use the mcv:validateEmail
validator. Based on this example, it will be simple to work with the rest of the validators.
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. In addition, we have used Apache MyFaces Commons 1.2, which is designed for JSF 1.2, but it seems that it supports JSF 2.0 also (as far as we have tested it, no problems occurred). You can download this distribution from http://myfaces.apache.org/commons/download.html. The Apache MyFaces Commons libraries (including necessary dependencies) are in the book code bundle, under the /JSF_libs/Apache MyFaces Commons JSF 2.0 folder
.
First you have to provide access to the Apache MyFaces Commons library. Knowing that this library has the namespace http://myfaces.apache.org/commons/validators and the most used prefix is mcv
, you can accomplish this task as shown next:
<%@taglib prefix="mcv" uri="http://myfaces.apache.org/commons/validators"%>
Next you can nest the corresponding validator inside an input tag, as shown next:
<h:inputText id="email1" value="#{user.email}" required="true"> <mcv:validateEmail/> </h:inputText> <h:message for="email1"/>
Apache MyFaces Commons validators follow the same pattern as an standard JSF converters. In practice, they are an extension of JSF validators, which means that you can do with them exactly what you can do with a JSF Core validator.
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: Apache_MyFaces_Commons_Validators
.
More details are available at:
http://myfaces.apache.org/commons12/myfaces-validators12/index.html