Reputation: 1845
I have created a validator in my project in the package "com.travstar.validators" called EmailValidator.
Content of the class:
package com.travstar.validators;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
@FacesValidator("emailValidator")
public class EmailValidator implements Validator
{
private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\." +
"[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*" +
"(\\.[A-Za-z]{2,})$";
private Pattern pattern;
private Matcher matcher;
public EmailValidator()
{
pattern = Pattern.compile(EMAIL_PATTERN);
}
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException
{
matcher = pattern.matcher(value.toString());
if (!matcher.matches())
{
FacesMessage msg = new FacesMessage("Email validation failed.", "Invalid Email format.");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
}
and I implement the email validation like this:
<h:panelGrid columns="1" width="100%">
<h:outputText value="Email Address"></h:outputText>
<h:inputText id="emailAddress" value="#{passengerDetailsBean.contactDetails.emailAddress}" styleClass="input-block-level" placeholder="" required="true">
<f:validator validatorId="emailValidator"></f:validator>
</h:inputText>
<h:message for="emailAddress" styleClass="alert alert-error" />
</h:panelGrid>
This is more or less exactly like a tutorial I found online, but I haven't been able to get it working.
The moment I hit the page, the following exception is thrown:
javax.servlet.ServletException: Unknown validator id 'emailValidator'
I'm using JSF 2.1, so as far as I know I am not required to fiddle with the faces.config.xml
Anyone have any ideas as to why this is happening?
Upvotes: 3
Views: 2807
Reputation: 1109745
Apparently the target server is agressively caching its deploy/work folder and not immediately noticing any external changes. Which one are you using? Does it have kind of "development" and "production" stage setting? Set it to development.
Upvotes: 2