Exemplo n.º 1
0
  /**
   * 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());
        }
      }
    }
  }
Exemplo n.º 2
0
 /**
  * Applying the given validation rules on the given property of the given object. The validation stops as soon as
  * one of the validation rules produces validation errors. This errors are then registered with the given
  * {@link Errors) instance.
  *
  * @param rules The validation rules that should be applied on the given property of the given object.
  * @param propertyName The name of the property to be validated.
  * @param obj The validated object.
  * @param errors The {@link Errors} instance where the validation errors will be registered.
  */
 protected void validateAndShortCircuitRules(
     ValidationRule[] rules, String propertyName, Object obj, Errors errors) {
   for (int i = 0; i < rules.length; i++) {
     ValidationRule rule = rules[i];
     if (rule.isApplicable(obj) && !rule.getCondition().check(obj)) {
       String errorCode =
           errorCodeConverter.convertPropertyErrorCode(
               rule.getErrorCode(), obj.getClass(), propertyName);
       errors.rejectValue(
           propertyName, errorCode, rule.getErrorArguments(obj), rule.getDefaultErrorMessage());
       if (shortCircuitFieldValidation) {
         return;
       }
     }
   }
 }