See Creating a Custom Validator in the JEE5 Tutorial. The Creating a Custom Tag section details how to implement your ValidatorELTag
class.
for="myControl"
I doubt you'll need this attribute (I'm not sure how you'd make use of it). The validator will be set on the parent control. for
attributes are usually only used when one control refers to another, as in the label component.
EDIT: I misread the question; the answer above applies to JSPs (those tag-related classes in core JSF 1.2 are for JSPs only; Facelets has its own tag system; the good news is that you don't need a Java class specifically for defining the tag).
Sample validator:
public class RegexValidator implements Validator, StateHolder {
private boolean isTransient;
private String regex;
public String getRegex() { return regex; }
public void setRegex(String regex) { this.regex = regex; }
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
//TODO: throw ValidatorException if not valid
}
//TODO: implement remaining StateHolder methods...
}
This validator is then registered in the faces-config.xml
:
<validator>
<validator-id>regex.validator</validator-id>
<validator-class>val.RegexValidator</validator-class>
</validator>
You then add a tag library to the app (e.g. WEB-INF/facelets/foo.taglib.xml
):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib xmlns="http://java.sun.com/JSF/Facelet">
<namespace>http://demo</namespace>
<tag>
<tag-name>regexValidator</tag-name>
<validator>
<validator-id>regex.validator</validator-id>
</validator>
</tag>
</facelet-taglib>
Add a xmlns:demo="http://demo"
declaration to any Facelets views you want to use the tag library in; your validator tag will start in the form <demo:regexValidator ...
; attributes will be picked up through introspection of the validator class.