I'm using JSF and I'm building a register web page. I'm trying to use a validator to check if the username that the user selected is already existing or not. But I don't find how to query the database to check the username. I've tryed with EJB injection but it is not working, I've got an null pointer exception.
@FacesValidator("usernameUnicityValidator")
public class UsernameUnicityValidator implements Validator {
@EJB
private ArticleFacadeLocal articleFacade;
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if (value==null) {
FacesMessage message = new FacesMessage("The username cannot be empty");
message.setSeverity(FacesMessage.SEVERITY_FATAL);
throw new ValidatorException(message);
}
if (userFacade.findByUsername((String)value)) {
FacesMessage message = new FacesMessage("Username already exist");
throw new ValidatorException(message);
}
}
}