@Override public String getNestedPath() { return this.source.getNestedPath(); }
@Override public String getNestedPath() { return this.source.getNestedPath(); }
assertEquals("spouse.", errors.getNestedPath()); assertEquals("argh", errors.getFieldValue("age")); Validator spouseValidator = new SpouseValidator(); assertEquals("", errors.getNestedPath()); errors.pushNestedPath("spouse"); assertEquals("spouse.", errors.getNestedPath()); errors.pushNestedPath("spouse"); assertEquals("spouse.spouse.", errors.getNestedPath()); errors.popNestedPath(); assertEquals("spouse.", errors.getNestedPath()); errors.popNestedPath(); assertEquals("", errors.getNestedPath()); try { errors.popNestedPath(); assertEquals("spouse.", errors.getNestedPath()); errors.setNestedPath(""); assertEquals("", errors.getNestedPath()); try { errors.popNestedPath(); assertEquals("spouse.", errors.getNestedPath());
@Test public void testValidatorWithNestedObjectNull() { TestBean tb = new TestBean(); Errors errors = new BeanPropertyBindingResult(tb, "tb"); Validator testValidator = new TestBeanValidator(); testValidator.validate(tb, errors); errors.setNestedPath("spouse."); assertEquals("spouse.", errors.getNestedPath()); Validator spouseValidator = new SpouseValidator(); spouseValidator.validate(tb.getSpouse(), errors); errors.setNestedPath(""); assertTrue(errors.hasFieldErrors("spouse")); assertEquals(1, errors.getFieldErrorCount("spouse")); assertEquals("SPOUSE_NOT_AVAILABLE", errors.getFieldError("spouse").getCode()); assertEquals("tb", (errors.getFieldErrors("spouse").get(0)).getObjectName()); assertEquals(null, (errors.getFieldErrors("spouse").get(0)).getRejectedValue()); }
assertEquals("spouse.", errors.getNestedPath()); Validator spouseValidator = new SpouseValidator(); spouseValidator.validate(tb.getSpouse(), errors);
public String getNestedPath() { return this.source.getNestedPath(); }
@Override public String getNestedPath() { return this.source.getNestedPath(); }
@Override public String getNestedPath() { return this.source.getNestedPath(); }
protected void pushNestedPath(String newPath, Errors errors) { String currentPath = errors.getNestedPath(); nestedPathStack.push(currentPath); errors.setNestedPath(currentPath + newPath); }
errors.getNestedPath() + constraintPropertyName, getPropertyValue(errors, target), false,
errors.getNestedPath() + constraintPropertyName, getPropertyValue(errors, target), false,
/** * Applies the global validation rules as listed in the given validation configuration on the given object, and * registering all global validation errors with the given {@link Errors}. * * @param configuration The bean validation configuration that holds all the global validation rules. * @param obj The validated object. * @param errors The {@link Errors} instance where all global validation errors will be registered. */ protected void applyGlobalValidationRules(BeanValidationConfiguration configuration, Object obj, Errors errors) { ValidationRule[] globalRules = configuration.getGlobalRules(); for (int i = 0; i < globalRules.length; i++) { ValidationRule rule = globalRules[i]; if (rule.isApplicable(obj) && !rule.getCondition().check(obj)) { String errorCode = errorCodeConverter.convertGlobalErrorCode(rule.getErrorCode(), obj.getClass()); // if there is a nested path in errors, the global errors should be registered as field errors // for the nested path. Otherwise, they should be registered as global errors. Starting from Spring 2.0-rc2 // this is actually not required - it's just enough to call rejectValue() with null as the field name, // but we keep this implementation for now to support earlier versions. if (StringUtils.hasLength(errors.getNestedPath())) { String nestedPath = errors.getNestedPath(); String propertyName = nestedPath.substring(0, nestedPath.length() - 1); errors.popNestedPath(); errors.rejectValue(propertyName, errorCode, rule.getErrorArguments(obj), rule.getDefaultErrorMessage()); errors.pushNestedPath(propertyName); } else { errors.reject(errorCode, rule.getErrorArguments(obj), rule.getDefaultErrorMessage()); } } } }
logger.debug("Validating object in path '" + errors.getNestedPath() + "'");
String nestedPath = errors.getNestedPath(); try { errors.setNestedPath(buildNestedPath(nestedPath, propertyName, indexOrKey)); validatePropertyWithConstraint(errors.getNestedPath() + associatedPropertyName, associatedObject, errors, new BeanWrapperImpl(associatedObject), associatedConstraintedProperties);