Java Reference
In-Depth Information
if (!(component instanceof UIInput)) {
return;
}
if ( null == formatPatternsList || null == toValidate) {
return;
}
value = toValidate.toString();
// validate the value against the list of valid patterns.
Iterator patternIt = formatPatternsList.iterator();
while (patternIt.hasNext()) {
valid = isFormatValid(
((String)patternIt.next()), value);
if (valid) {
break;
}
}
if ( !valid ) {
FacesMessage errMsg =
new FacesMessage(FORMAT_INVALID_MESSAGE_ID);
FacesContext.getCurrentInstance().addMessage(null, er-
rMsg);
throw new ValidatorException(errMsg);
}
}
}
The
@FacesValidator
annotation registers the
FormatValidator
class as a val-
idator with the JavaServer Faces implementation. The
validate
method gets the local
value of the component and converts it to a
String
. It then iterates over the
form-
atPatternsList
list, which is the list of acceptable patterns that was parsed from the
formatPatterns
attribute of the custom validator tag.
While iterating over the list, this method checks the pattern of the component's local
value against the patterns in the list. If the pattern of the local value does not match
any pattern in the list, this method generates an error message. It then creates a
javax.faces.application.FacesMessage
and queues it on the
FacesCon-
text
for display during the Render Response phase, using a
String
that represents the
key in the
Properties
file:
public static final String FORMAT_INVALID_MESSAGE_ID =
"FormatInvalid";
}