Creating a custom validator
As you design dynamic Web pages, you might want to use custom validators to validate data that you cannot validate 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.
Here is one way that you can create a custom validator in your Web project.
- Create a Web project (File > New > Dynamic Web Project).
- Create a new Faces JSP page (File > New > Faces JSP File).
- In the Project Explorer view, open the Java Source folder for the Web project.
- Right-click the Java Source folder and then click New > Package.
- 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 Source folder.
- Right-click the package named validator and then click New > Class.
- In the "New Java Class" dialog:
- 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. Add the following code to the file:
<validator> <validator-id>MyValidator<validator-id> <validator-class>validator.MyValidator<validator-class> <validator>- Add an Input text field to the page you created in Step 2.
- In Source mode 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 Command - Button component to the page.
- Run the page on server. When the command button is clicked, "my custom validator" displays on the console.
Related concepts
JavaServer Faces
Related tasks
Adding validation to Faces input components
Creating Faces applications - overview
Creating a Faces JSP file
JavaServer Faces life cycle
Related reference
Faces components reference
Attributes for Faces components