Creating a custom validator

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.
  1. Create a JSF page (click File > New > Web Page 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.
  2. In the Project Explorer view, right-click the Web project's Java Resources: src folder and select New > Package.
  3. 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.
  4. Right-click the package named validator and then click New > Class. The New Java Class wizard opens.
  5. In the New Java Class wizard:
    1. Name the class MyValidator.
    2. Click Add.
    3. 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.
      
      	 }
  6. 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.

  7. Add an Input text field to the page you created in Step 1.
  8. 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>
  9. Add a Button - Command component to the page.
  10. Run the page on server. When the command button is clicked, my custom validator displays on the console.

Feedback