Java Reference
In-Depth Information
The constructor of ValidatorException takes an instance of javax.faces.
application.FacesMessage as a parameter. This object is used to display the error
message on the page when validation fails. The message to display is passed as a
String to the constructor of FacesMessage . In our example, if the label attribute
of the component is not null nor empty, we use it as part of the error message,
otherwise we use the value of the component's id attribute. This behavior follows
the pattern established by standard JSF validators.
Before we can use our custom validator in our pages, we need to declare it in the
application's faces-config.xml configuration file. To do so, we need to add a
<validator> element just before the closing </faces-config> element.
<validator>
<validator-id>emailValidator</validator-id>
<validator-class>
com.ensode.jsf.validators.EmailValidator
</validator-class>
</validator>
The body of the <validator-id> sub element must contain a unique identifier for
our validator. The value of the <validator-class> element must contain the fully
qualified name of our validator class.
Once we add our validator to the application's faces-config.xml , we are ready to
use it in our pages.
In our particular case, we need to modify the email field to use our custom validator.
<h:inputText id="email" label="Email Address"
required="true" value="#{RegistrationBean.email}">
<f:validator validatorId="emailValidator"/>
</h:inputText>
All we need to do is nest an <f:validator> tag inside the input field we wish to
have validated using our custom validator. The value of the validatorId attribute
of <f:validator> must match the value of the body of the <validator-id> element
in faces-config.xml .
 
Search WWH ::




Custom Search