Monday, August 15, 2011

JSF custom validator

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.test;
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;

/**
*
* @author Sajjad
*/
@FacesValidator("com.test.Recp")
public class RecpValidator implements Validator{
public void validate(FacesContext context, UIComponent component,Object value) {
if(value == null) return;
String valString=value.toString();
if(valString.length()!=6) {
FacesMessage message = new FacesMessage();
// com.corejsf.util.Messages.getMessage("com.corejsf.messages", "badLuhnCheck", null);
message.setSeverity(FacesMessage.SEVERITY_ERROR);
message.setSummary("String length must be 6");
message.setDetail("String length must be 6");

throw new ValidatorException(message);
}
}
}