1 16 package org.jmanage.webui.validator; 17 18 import org.apache.commons.validator.ValidatorAction; 19 import org.apache.commons.validator.Field; 20 import org.apache.commons.validator.ValidatorUtil; 21 import org.apache.commons.validator.GenericValidator; 22 import org.apache.struts.action.ActionErrors; 23 import org.apache.struts.validator.Resources; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import java.io.Serializable ; 27 28 32 public class JManageFieldChecks implements Serializable { 33 34 private static final String FIELD_TEST_NULL = "NULL"; 35 private static final String FIELD_TEST_NOTNULL = "NOTNULL"; 36 private static final String FIELD_TEST_EQUAL = "EQUAL"; 37 private static final String FIELD_TEST_NOT_EQUAL = "NOTEQUAL"; 38 39 48 public static boolean validateMatch(Object bean, 49 ValidatorAction validatorAction, 50 Field field, ActionErrors errors, 51 HttpServletRequest request) { 52 53 String value = ValidatorUtil.getValueAsString(bean, field.getProperty()); 54 String sProperty2 = field.getVarValue("secondProperty"); 55 String value2 = ValidatorUtil.getValueAsString(bean, sProperty2); 56 57 if (!GenericValidator.isBlankOrNull(value) && 58 !GenericValidator.isBlankOrNull(value2) && 59 !value.equals(value2)) { 60 errors.add(field.getKey(), 61 Resources.getActionError(request, validatorAction, 62 field)); 63 return false; 64 } 65 return true; 66 } 67 68 69 84 public static boolean validateRequiredIf(Object bean, ValidatorAction va, 85 Field field, ActionErrors errors, 86 org.apache.commons.validator.Validator validator, 87 HttpServletRequest request) { 88 Object form = validator.getResource( 89 org.apache.commons.validator.Validator.BEAN_KEY); 90 String value = null; 91 boolean required = false; 92 if(isString(bean)){ 93 value = (String )bean; 94 }else{ 95 value = ValidatorUtil.getValueAsString(bean, field.getProperty()); 96 } 97 int i = 0; 98 String fieldJoin = "AND"; 99 if(!GenericValidator.isBlankOrNull(field.getVarValue("fieldJoin"))){ 100 fieldJoin = field.getVarValue("fieldJoin"); 101 } 102 if(fieldJoin.equalsIgnoreCase("AND")){ 103 required = true; 104 } 105 while(!GenericValidator.isBlankOrNull(field.getVarValue("field"+i))){ 106 String dependProp = field.getVarValue("field"+i); 107 String dependTest = field.getVarValue("fieldTest"+i); 108 String dependTestValue = field.getVarValue("fieldValue"+i); 109 String dependVal = null; 110 boolean thisRequired = false; 111 112 dependVal = ValidatorUtil.getValueAsString(form, dependProp); 113 if(dependTest.equals(FIELD_TEST_NULL)){ 114 if((dependVal != null) && (dependVal.length() > 0)){ 115 thisRequired = false; 116 }else{ 117 thisRequired = true; 118 } 119 }else if(dependTest.equals(FIELD_TEST_NOTNULL)){ 120 if((dependVal != null) && (dependVal.length() > 0)){ 121 thisRequired = true; 122 }else{ 123 thisRequired = false; 124 } 125 }else if(dependTest.equals(FIELD_TEST_EQUAL)){ 126 thisRequired = dependTestValue.equalsIgnoreCase(dependVal); 127 }else if(dependTest.equals(FIELD_TEST_NOT_EQUAL)){ 128 thisRequired = !dependTestValue.equalsIgnoreCase(dependVal); 129 } 130 131 if(fieldJoin.equalsIgnoreCase("AND")){ 132 required = required && thisRequired; 133 }else{ 134 required = required || thisRequired; 135 } 136 i++; 137 } 138 if(required){ 139 if(GenericValidator.isBlankOrNull(value)){ 140 errors.add(field.getKey(), 141 Resources.getActionError(request, va, field)); 142 return false; 143 }else{ 144 return true; 145 } 146 } 147 return true; 148 } 149 150 157 protected static boolean isString(Object o) { 158 return (o == null) ? true : String .class.isInstance(o); 159 } 160 } | Popular Tags |