As you design dynamic Web pages, you might want to use custom validators
for data that you verify in other ways. A custom validator looks at the submitted
value and then performs validation on it. For example, you can add a custom
validator to an input text field so that the validator ensures that the submitted
value contains nine numbers. If the submitted value does not contain nine
numbers, the custom validator displays an error message when the page is rendered.
Before you create a Faces JSP file you must create a Dynamic Web Project
that is enabled for Faces technology. For more information, refer to
Developing Faces (JSF) applications.
Here is one way that you can create a custom validator in your Web
project.
- Create a JSF page (click then
follow the instructions in the New Web Page wizard). For more information
on creating a JSF page refer to Creating a Faces JSP file.
- In the Project Explorer view, right-click the Web project's Java
Resources: src folder and select .
- In the New Java Package dialog, name the package validator and
click Finish. A new package named
validator is created and is displayed in the Java™ Resources: src folder.
- Right-click the package named validator and then click . The New
Java Class wizard opens.
- In the New Java Class wizard:
- Name the class MyValidator.
- Click Add.
- In the Implemented Interfaces Selections dialog
that displays, enter javax.faces.validator.Validator in
the Choose Interfaces field. Click OK. Click Finish. The file, MyValidator.java, is created in the validator folder
and contains code similar to the following Java code:
package validator;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
public class MyValidator implements Validator {
/* (non-Javadoc)
* @see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
*/
public void validate(FacesContext arg0, UIComponent arg1, Object arg2)
throws ValidatorException {
// TODO Auto-generated method stub
}
public void validate(FacesContext arg0, UIComponent arg1, Object arg2)
throws ValidatorException {
System.out.println("my custom validator");
//TODO: Add your custom validation code here. This code will be execured during the validation
//phase of the JSF lifecycle. If any validation errors occur you can throw an exception message
//that in turn can be displayed using a h:message (or h:messages) component.
}
- In the WEB-INF folder of the same project, open the file named
faces-config.xml. In the Source view, add the following code to the file before </faces-config>:
<validator>
<validator-id>MyValidator</validator-id>
<validator-class>validator.MyValidator</validator-class>
</validator>
Save faces-config.xml.
- Add an Input text field to the page you created in Step 1.
- In the Source view for the page, add the validator tag as a child
of inputText and set the validatorId attribute to MyValidator, as follows:
<h:inputText styleClass="inputText" id="text1"><f:validator validatorId="MyValidator"></f:validator></h:inputText>
- Add a Button - Command component to the page.
- Run the page on server. When the command button is clicked, my
custom validator displays on the console.