For example: I have a JSF Validator to validate e-mail thusly:
@FacesValidator(value="validateEmail")
public class Email implements Validator
{
private static final String EMAIL_REGEXP =
"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
@Override
public void validate(FacesContext context, UIComponent c, Object val) throws ValidatorException
{
String email = (String) val;
Pattern mask = null;
mask = Pattern.compile(EMAIL_REGEXP);
Matcher matcher = mask.matcher(email);
if (!matcher.matches()) {
FacesMessage message = new FacesMessage();
message.setDetail("Must be of the form xxx@yyy.zzz");
message.setSummary("E-mail Addresss not valid");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
}
}
This validator will throw an exception if the user doesn't enter an e-mail. However sometimes I want to make the e-mail an optional field. Is there a way to do this that doesn't require me to write a different validator?
Ideally I would like it to check for a parameter somewhere in the JSF markup that uses it.