0

I have an assignment for university where we shall validate form input with AJAX and a Validator Class. Now, I have used AJAX in JSF for filtering a list. But I don't know how to combine it with a validator. The validation should happen when user leaves input field. The validator should check if email input matches a regex pattern and if there is an existing user with the same email. If validation fails, a FacesMessage should be shown. How do I connect the dots?

This is part of a bigger project and I have tried to include only the relevant code.

FILE STRUCTURE (only relevant part)

.
├── java
│   ├── dao
│   ├── logic
│   ├── presentation
│   │   ├── managedbeans
│   │   │   └── UserBean.java
│   │   └── validator
│   │       └── Email.java
│   │
│   └── transfer
└── webapp
    └── signup.xhtml

THE CODE

signup.xhtml only relevant part of the form

<h:form id="registerForm" class="col l6 s12 center">

<h:outputLabel for="eMail" value="Email" class="active" />
  <h:inputText 
    id="eMail" 
    value="#{user.email}" 
    class="validate"
    requiredMessage="Email is required" 
    label="eMail" 
    maxlength="25"
    alt="email" 
    autocomplete="on" 
    required="true">  

    <f:validator validatorId="emailValidator"/> 
    <f:ajax event="change" listener="#{user.validateEmail}" execute="eMail" render="emailError"></f:ajax>

  </h:inputText>
  <h:message for="eMail" id="emailError" />

  <h:commandButton value="Sign up" action="#{user.addUser}" class="waves-effect waves-light btn" type="submit" />

</h:form>

Email.java

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 Email implements Validator { 

  private static final Pattern EMAIL_PATTERN = Pattern.compile("[\\w\\.-]*[a-zA-Z0-9_]@[\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]");

  @Override
  public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {

    if( value == null) {
      return;
    }

    String email = (String) value;
    boolean matches = EMAIL_PATTERN.matcher(email).matches();

    if(!matches) {
      FacesMessage msg = new FacesMessage(
          FacesMessage.SEVERITY_FATAL,
          "Email is invalid",
          null);
      throw new ValidatorException(msg);
    } 
  }
}

UserBean.java So, I thought it might make sense to call a function validateEmail in the UserBean and then call the validate function within the Validator Class. But I am really not sure about this. How would I pass the parameters context, component and value?

public void validateEmail(AjaxBehaviorEvent e) {
//    Email emailValidator = new Email();
//    emailValidator.validate(context, component, value);
}
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Moglash
  • 259
  • 1
  • 5
  • 23

1 Answers1

0

You could validate your email in your XHTML files.

For that, have a look on this: Email validation using regular expression in JSF 2 / PrimeFaces

So you only actually need to validate, if Username or Email already exists. You can do that in a validation class for the University.

If you have a validation in your UserManager already, you can use that in the validation class.

regards