com.evasion.common.validator.PasswordValidator Maven / Gradle / Ivy
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.evasion.common.validator;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
/**
*
* @author sebastien.glon
*/
@FacesValidator(value = "passwordValidator")
public class PasswordValidator implements Validator {
@Override
public void validate(FacesContext fc, UIComponent uic, Object o) throws ValidatorException {
// Obtain the client ID of the first password field from f:attribute.
String passwordId = (String) uic.getAttributes().get("passwordId");
// Find the actual JSF component for the client ID.
UIInput passwordInput = (UIInput) fc.getViewRoot().findComponent(passwordId);
// Get its value, the entered password of the first field.
String password = (String) passwordInput.getValue();
// Cast the value of the entered password of the second field back to String.
String confirm = (String) o;
// Compare the first password with the second password.
if (!password.equals(confirm)) {
throw new ValidatorException(new FacesMessage("Passwords are not equal."));
}
// You can even validate the minimum password length here and throw accordingly.
// Or, if you're smart, calculate the password strength and throw accordingly ;)
}
}