Apache MyFaces
Project Documentation
Foundation

Creating Custom Validators

A JSF Validator needs the following elements:

  • A class implementing validator interface.
  • Entry on faces-config.xml.
  • Tag entry on tld.
  • Tag entry on facelets taglib (optional).
  • JSP Tag class

Myfaces builder plugin provide annotations/doclets to help users maintain configuration files, and generating both validator classes and validator jsp tag classes automatically. In this way, all information for a validator is just in one file (the class implementing Validator interface).

For both jsf versions 1.1 and 1.2, it is necessary to provide base classes for validators and tag classes, where all generated should inherit. This class is on myfaces commons validators project, but users can provide custom templates implementations for it.

It is possible to use abstract pattern for create validator classes, in similar way to components.

Setting up your project

All information related can be found here.

Configuration files (faces-config.xml, .tld, facelets taglib)

This page shows some examples.

Annotate Validator Classes

Just add the annotations/doclets as presented below:

 @JSFValidator(
   name = "mcv:validateEmail",
   tagClass = "org.apache.myfaces.commons.validator.ValidateEmailTag")
public class EmailValidator extends org.apache.myfaces.commons.validator.ValidatorBase
{

    public static final String VALIDATOR_ID = "org.apache.myfaces.commons.validator.Email";
 
    /** .......... impl code goes here ......... **/
    
    @JSFProperty
    public String getProperty()
    {
        //Some stuff
    }   
}

Write a Validator Class using Abstract Pattern

See myfaces commons validators project for examples.