package be.redtree.bean;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import be.redtree.model.Field;
import be.redtree.sql.FormController;
@ManagedBean
@RequestScoped
public class ValidatorBean implements Serializable {
private static Log sLog = LogFactory.getLog(ValidatorBean.class);
private FormController formController = new FormController();
public ValidatorBean() {
}
public void validateTextField(FacesContext ctx, UIComponent component, Object value) throws ValidatorException {
Long fieldId = (Long) component.getAttributes().get("field");
Field field = formController.getField(fieldId);
String temp = value.toString();
if (!field.getMandatory() || (temp != null && temp.trim().length() > 0)) {
return;
}
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, field.getMessage(), ""));
}
public void validateCheckboxField(FacesContext ctx, UIComponent component, Object value) throws ValidatorException {
Long fieldId = (Long) component.getAttributes().get("field");
Field field = formController.getField(fieldId);
String temp = value.toString();
if (!field.getMandatory() || (temp != null && temp.trim().length() > 0)) {
return;
}
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, field.getMessage(), ""));
}
public void validateRadioField(FacesContext ctx, UIComponent component, Object value) throws ValidatorException {
Long fieldId = (Long) component.getAttributes().get("field");
Field field = formController.getField(fieldId);
String temp = value.toString();
if (!field.getMandatory() || (temp != null && temp.trim().length() > 0)) {
return;
}
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, field.getMessage(), ""));
}
public void validateDropdownField(FacesContext ctx, UIComponent component, Object value) throws ValidatorException {
Long fieldId = (Long) component.getAttributes().get("field");
Field field = formController.getField(fieldId);
String temp = value.toString();
if (!field.getMandatory() || (temp != null && temp.trim().length() > 0)) {
return;
}
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, field.getMessage(), ""));
}
}