//by scud start /** * Finds all ScriptValidationAware validators that apply to the field covered by this tag. * * @param formTag the parent form tag this tag is in * @param fieldName the name of the field to validate (used for error message key) * @param fieldClass the Class of the object the field is for * @param propertyName the actual property name to get validator for; if null, fieldName is used */ private void findScriptingValidators(Form formTag, String fieldName, Class fieldClass, String propertyName) { List validators = ActionValidatorManager.getValidators(fieldClass, formTag.getActionName()); String name = fieldName;
if (propertyName != null) { name = propertyName; }
for (Iterator iterator = validators.iterator(); iterator.hasNext();) { Validator validator = (Validator) iterator.next();
if (!(validator instanceof ScriptValidationAware)) { continue; }
ValidatorContext validatorContext = new DelegatingValidatorContext(fieldClass);
if (validator instanceof FieldValidator) { FieldValidator fieldValidator = (FieldValidator) validator;
// JavaScriptVisitorFieldValidators must validate model, not action if (validator instanceof JavaScriptVisitorFieldValidator) { JavaScriptVisitorFieldValidator visitorValidator = (JavaScriptVisitorFieldValidator) validator; String propName = null; boolean visit;
if (visitorValidator.getFieldName().equals("model") && ModelDriven.class.isAssignableFrom(fieldClass)) { visit = true; } else { String baseName = name; int idx = name.indexOf(".");
if (idx != -1) { baseName = name.substring(0, idx); propName = name.substring(idx + 1); }
visit = baseName.equals(visitorValidator.getFieldName()); }
if (visit) { Class realFieldClass = visitorValidator.getValidatedClass();
if (realFieldClass == null) { for (Iterator iterator1 = getStack().getRoot().iterator(); iterator1.hasNext();) { Object o = iterator1.next(); try { PropertyDescriptor pd = OgnlRuntime.getPropertyDescriptor(o.getClass(), visitorValidator.getFieldName()); realFieldClass = pd.getPropertyType(); break; } catch (Throwable t) { // just keep trying } } }
if (realFieldClass != null) { if (visitorValidator.isAppendPrefix()) { findScriptingValidators(formTag, visitorValidator.getFieldName() + "." + name, realFieldClass, propName); } else { findScriptingValidators(formTag, name, realFieldClass, propName); } } else { LOG.warn("Cannot figure out class of visited object"); } } } else if (fieldValidator.getFieldName().equals(name)) { validator.setValidatorContext(validatorContext); formTag.registerValidator((ScriptValidationAware) fieldValidator, new HashMap(getParameters())); } } else { validator.setValidatorContext(validatorContext); formTag.registerValidator((ScriptValidationAware) validator, new HashMap(getParameters())); } } }
/** * will validator by javascript . * * @return true if set in webwork.properties,else false */ public static boolean canJSValidator() { String propString = "jsvalidator"; if(Configuration.isSet(propString)) { return "true".equals(Configuration.getString(propString)); } return false; } //by scud end
|