1 16 package org.apache.myfaces.custom.equalvalidator; 17 18 import javax.faces.FacesException; 19 import javax.faces.application.FacesMessage; 20 import javax.faces.component.StateHolder; 21 import javax.faces.component.UIComponent; 22 import javax.faces.component.UIInput; 23 import javax.faces.context.FacesContext; 24 import javax.faces.validator.Validator; 25 import javax.faces.validator.ValidatorException; 26 27 import org.apache.myfaces.util.MessageUtils; 28 29 47 public class EqualValidator implements Validator, StateHolder { 48 49 52 public static final String VALIDATOR_ID = "org.apache.myfaces.validator.Equal"; 53 54 58 public static final String EQUAL_MESSAGE_ID = "org.apache.myfaces.Equal.INVALID"; 59 60 public EqualValidator(){ 61 } 62 63 private String _for= null; 65 66 67 private boolean _transient = false; 69 70 71 public void validate( 73 FacesContext facesContext, 74 UIComponent uiComponent, 75 Object value) 76 throws ValidatorException { 77 78 if (facesContext == null) throw new NullPointerException ("facesContext"); 79 if (uiComponent == null) throw new NullPointerException ("uiComponent"); 80 81 if (value == null) 82 { 83 return; 84 } 85 86 UIInput foreignComp = (UIInput) uiComponent.getParent().findComponent(_for); 87 if(foreignComp==null) 88 throw new FacesException("Unable to find component '" + _for + "' (calling findComponent on component '" + uiComponent.getId() + "')"); 89 90 Object [] args = {value.toString(),(foreignComp.getValue()==null) ? foreignComp.getId():foreignComp.getValue().toString()}; 91 92 if(foreignComp.getValue()==null || !foreignComp.getValue().toString().equals(value.toString()) ) 93 throw new ValidatorException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,EQUAL_MESSAGE_ID, args)); 94 95 } 96 98 public Object saveState(FacesContext context) { 99 Object state = _for; 100 return state; 101 } 102 103 public void restoreState(FacesContext context, Object state) { 104 _for = (String ) state; 105 } 106 107 public boolean isTransient() { 108 return _transient; 109 } 110 111 public void setTransient(boolean newTransientValue) { 112 _transient = newTransientValue; 113 } 114 116 119 public String getFor() { 120 return _for; 121 } 122 123 126 public void setFor(String string) { 127 _for = string; 128 } 129 130 } | Popular Tags |