@Override public void popNestedPath() throws IllegalStateException { this.source.popNestedPath(); }
@Override public void popNestedPath() throws IllegalStateException { this.source.popNestedPath(); }
errors.popNestedPath();
public void validate(Customer customer, String password, String passwordConfirm, Errors errors) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "passwordConfirm", "passwordConfirm.required"); errors.pushNestedPath("customer"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "firstName.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "lastName.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "emailAddress", "emailAddress.required"); errors.popNestedPath(); if (errors.hasErrors()){ if (!passwordConfirm.equals(password)) { errors.rejectValue("passwordConfirm", "invalid"); } if (!customer.getFirstName().matches(getValidNameRegex())) { errors.rejectValue("firstName", "firstName.invalid", null, null); } if (!customer.getLastName().matches(getValidNameRegex())) { errors.rejectValue("lastName", "lastName.invalid", null, null); } if (!customer.getPassword().matches(getValidPasswordRegex())) { errors.rejectValue("password", "password.invalid", null, null); } if (!password.equals(passwordConfirm)) { errors.rejectValue("password", "passwordConfirm.invalid", null, null); } if (!GenericValidator.isEmail(customer.getEmailAddress())) { errors.rejectValue("emailAddress", "emailAddress.invalid", null, null); } } }
errors.pushNestedPath("phone"); errors.rejectValue("phoneNumber", "phoneNumber.duplicate", null); errors.popNestedPath(); errors.pushNestedPath("phone"); errors.rejectValue("id", "phone.invalid_id", null); errors.popNestedPath();
public void validate(Object obj, Errors errors, boolean useEmailForUsername) { RegisterCustomerForm form = (RegisterCustomerForm) obj; Customer customerFromDb = customerService.readCustomerByUsername(form.getCustomer().getUsername()); if (customerFromDb != null && customerFromDb.isRegistered()) { if (useEmailForUsername) { errors.rejectValue("customer.emailAddress", "emailAddress.used", null, null); } else { errors.rejectValue("customer.username", "username.used", null, null); } } ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "passwordConfirm", "passwordConfirm.required"); errors.pushNestedPath("customer"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "firstName.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "lastName.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "emailAddress", "emailAddress.required"); errors.popNestedPath(); if (!errors.hasErrors()) { if (!form.getPassword().matches(getValidatePasswordExpression())) { errors.rejectValue("password", "password.invalid", null, null); } if (!form.getPassword().equals(form.getPasswordConfirm())) { errors.rejectValue("password", "passwordConfirm.invalid", null, null); } if (!GenericValidator.isEmail(form.getCustomer().getEmailAddress())) { errors.rejectValue("customer.emailAddress", "emailAddress.invalid", null, null); } } }
errors.pushNestedPath("spouse"); assertEquals("spouse.spouse.", errors.getNestedPath()); errors.popNestedPath(); assertEquals("spouse.", errors.getNestedPath()); errors.popNestedPath(); assertEquals("", errors.getNestedPath()); try { errors.popNestedPath(); assertEquals("", errors.getNestedPath()); try { errors.popNestedPath();
errors.pushNestedPath("identifiers[" + index + "]"); patientIdentifierValidator.validate(identifier, errors); errors.popNestedPath(); index++;
@Override public void popNestedPath() throws IllegalStateException { this.source.popNestedPath(); }
@Override public void popNestedPath() throws IllegalStateException { this.source.popNestedPath(); }
public void popNestedPath() throws IllegalStateException { this.source.popNestedPath(); }
errors.popNestedPath(); errors.popNestedPath();
atLeastOneNonVoidPersonNameLeft = true; errors.popNestedPath(); index++; errors.popNestedPath(); index++;
public class ApplicationUserFormValidator implements Validator { public void validate(Object target, Errors errors) { ApplicationUserForm f = (ApplicationUserForm) target; if (!f.getConfirmPassword().equals(f.getUser().getPassword())) ... errors.pushNestedPath("user"); new ApplicationUserValidator().validate(f.getUser(), errors); errors.popNestedPath(); } ... }
personValidator.validate(person, errors); } finally { errors.popNestedPath();
import org.springframework.validation.ValidationUtils; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.collections.CollectionUtils; public static void invokeValidatorForNestedCollection(Validator validator, Object obj, String collectionPath, Errors errors) { Collection collection; try { collection = (Collection) PropertyUtils.getProperty(obj, collectionPath); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new RuntimeException(e); } if (CollectionUtils.isEmpty(collection)) return; int counter = 0; for (Object elem : collection) { errors.pushNestedPath(String.format(collectionPath + "[%d]", counter)); ValidationUtils.invokeValidator(validator, elem, errors); errors.popNestedPath(); counter++; } }
/** * Validates the elements within the given map property. * * @param root The root of the object graph that is being validated. * @param map The given map or set. * @param propertyName The name of the array property. * @param errors The {@link Errors} instance where all validation errors will be registered. * @param validatedObjects A registry of all objects that were already validated. */ protected void validateMapProperty(Object root, Map map, String propertyName, Errors errors, Set validatedObjects) { for (Iterator entries = map.entrySet().iterator(); entries.hasNext();) { Entry entry = (Entry) entries.next(); Object key = entry.getKey(); if (!(key instanceof String)) { // skipping validation of elements that are mapped to non-string keys for there is no proper // representation of such elements as property path. continue; } Object value = entry.getValue(); String nestedPath = propertyName + PROPERTY_KEY_PREFIX + String.valueOf(key) + PROPERTY_KEY_SUFFIX; errors.pushNestedPath(nestedPath); validateObjectGraphConstraints(root, value, errors, validatedObjects); errors.popNestedPath(); } }
/** * Validates the elements of the given array property. * * @param root The root of the object graph that is being validated. * @param array The given array. * @param propertyName The name of the array property. * @param errors The {@link Errors} instance where all validation errors will be registered. * @param validatedObjects A registry of all objects that were already validated. */ protected void validateArrayProperty( Object root, Object array, String propertyName, Errors errors, Set validatedObjects) { for (int i = 0; i < Array.getLength(array); i++) { String nestedPath = propertyName + PROPERTY_KEY_PREFIX + i + PROPERTY_KEY_SUFFIX; errors.pushNestedPath(nestedPath); validateObjectGraphConstraints(root, Array.get(array, i), errors, validatedObjects); errors.popNestedPath(); } }
/** * Validates the given nested property bean (sub-bean). * * @param root The root of the object graph that is being validated. * @param subBean The given nested property value (the sub-bean). * @param propertyName The name of the array property. * @param errors The {@link Errors} instance where all validation errors will be registered. * @param validatedObjects A registry of all objects that were already validated. */ protected void validatedSubBean( Object root, Object subBean, String propertyName, Errors errors, Set validatedObjects) { errors.pushNestedPath(propertyName); validateObjectGraphConstraints(root, subBean, errors, validatedObjects); errors.popNestedPath(); }