[jboss-user] [JBoss Seam] - Re: How to write a validator that compares two fields

chawax do-not-reply at jboss.com
Thu Oct 11 07:53:32 EDT 2007


I finally wrote my own findComponent method, which looks for recursively a component.

The method is like this :

public class JSFUtils {
  | 
  | 	public static UIComponent findComponent(UIComponent component, String id) {
  | 		if (component.getId().equals(id)) {
  | 			return component;
  | 		}
  | 		else {
  | 			List<UIComponent> children = component.getChildren();
  | 			for (UIComponent myComponent : children) {
  | 				UIComponent componentFound = findComponent(myComponent, id);
  | 				if (componentFound != null) return componentFound;
  | 			}
  | 			return null;
  | 		}
  | 	}
  | 
  | }

Then my validator class is like this :
public void validate(FacesContext context, UIComponent cmp, Object value)
  | 		throws ValidatorException {
  | 	String compareTo = (String) cmp.getAttributes().get("compareTo");
  | 	String messageKey = (String) cmp.getAttributes().get("message");
  | 	UIInput input = (UIInput) JSFUtils.findComponent(context.getViewRoot(), compareTo);
  | 	String otherValue = (String) input.getValue();
  | 	boolean error = false;
  | 	if (value != null) {
  | 		if (! value.equals(otherValue)) {
  | 			error = true;
  | 		}
  | 	}
  | 	else {
  | 		if (otherValue != null) {
  | 			error = true;
  | 		}
  | 	}
  | 	if (error) {
  | 		String msg = Messages.instance().get(messageKey);
  | 		throw new ValidatorException(new FacesMessage(msg));
  | 	}
  | }

Hope it will help people who need this.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4094007#4094007

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4094007



More information about the jboss-user mailing list