Añadidos custom Validators de JSF para validar NIF y email.
Configurada validación en cliente de email.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package managedbean.validators;
|
||||
|
||||
import java.util.Map;
|
||||
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;
|
||||
|
||||
import org.primefaces.validate.ClientValidator;
|
||||
|
||||
@FacesValidator("emailValidator")
|
||||
public class EmailValidator implements Validator, ClientValidator {
|
||||
|
||||
private final static String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
|
||||
private final static Pattern EMAIL_COMPILED_PATTERN = Pattern.compile(EMAIL_PATTERN);
|
||||
|
||||
public void validate(FacesContext context, UIComponent comp, Object value) throws ValidatorException {
|
||||
String strValue = "";
|
||||
|
||||
if (value != null)
|
||||
strValue = String.valueOf(value);
|
||||
|
||||
if (strValue.equals(""))
|
||||
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Correo electrónico no válido", "La dirección " + value + " no es una dirección válida"));
|
||||
|
||||
Matcher matcher = EMAIL_COMPILED_PATTERN.matcher((String) value);
|
||||
if (!matcher.matches())
|
||||
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Correo electrónico no válido", "La dirección " + value + " no es una dirección válida"));
|
||||
}
|
||||
|
||||
public Map<String, Object> getMetadata() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getValidatorId() {
|
||||
return "emailValidator";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package managedbean.validators;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
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;
|
||||
|
||||
import org.primefaces.validate.ClientValidator;
|
||||
|
||||
import managedbean.common.ValidationUtils;
|
||||
|
||||
@FacesValidator("nifValidator")
|
||||
public class NifValidator implements Validator, ClientValidator {
|
||||
|
||||
public void validate(FacesContext context, UIComponent comp, Object value) throws ValidatorException {
|
||||
String strValue = "";
|
||||
|
||||
if (value != null)
|
||||
strValue = String.valueOf(value);
|
||||
|
||||
if (strValue.equals(""))
|
||||
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "El NIF no es válido", "El NIF " + value + " no es válido"));
|
||||
|
||||
if (ValidationUtils.isValid(strValue) == false)
|
||||
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "El NIF no es válido", "El NIF " + value + " no es válido"));
|
||||
}
|
||||
|
||||
public Map<String, Object> getMetadata() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getValidatorId() {
|
||||
return "nifValidator";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user