Java Reference
In-Depth Information
Implementing the Validator Interface
A Validator implementation must contain a constructor, a set of accessor methods
for any attributes on the tag, and a validate method, which overrides the validate
method of the Validator interface.
The hypothetical FormatValidator class also defines accessor methods for setting
the formatPatterns attribute, which specifies the acceptable format patterns for
input into the fields. The setter method calls the parseFormatPatterns method,
which separates the components of the pattern string into a string array, formatPat-
ternsList .
Click here to view code image
public String getFormatPatterns() {
return (this.formatPatterns);
}
public void setFormatPatterns(String formatPatterns) {
this.formatPatterns = formatPatterns;
parseFormatPatterns();
}
In addition to defining accessor methods for the attributes, the class overrides the val-
idate method of the Validator interface. This method validates the input and also
accesses the custom error messages to be displayed when the String is invalid.
The validate method performs the actual validation of the data. It takes the
FacesContext instance, the component whose data needs to be validated, and the
value that needs to be validated. A validator can validate only data of a component that
implements javax.faces.component.EditableValueHolder .
Here is an implementation of the validate method:
Click here to view code image
@FacesValidator
public class FormatValidator implements Validator, StateHolder {
...
public void validate(FacesContext context, UIComponent compon-
ent,
Object toValidate) {
boolean valid = false;
String value = null;
if ((context == null) || (component == null)) {
throw new NullPointerException();
}
Search WWH ::




Custom Search