Class SimpleErrors

java.lang.Object
org.springframework.validation.SimpleErrors
All Implemented Interfaces:
Serializable, Errors

public class SimpleErrors extends Object implements Errors, Serializable
A simple implementation of the Errors interface, managing global errors and field errors for a top-level target object. Flexibly retrieves field values through bean property getter methods, and automatically falls back to raw field access if necessary.

Note that this Errors implementation comes without support for nested paths. It is exclusively designed for the validation of individual top-level objects, not aggregating errors from multiple sources. If this is insufficient for your purposes, use a binding-capable Errors implementation such as BeanPropertyBindingResult.

Since:
6.1
Author:
Juergen Hoeller
See Also:
  • Constructor Details

    • SimpleErrors

      public SimpleErrors(Object target)
      Create a new SimpleErrors holder for the given target, using the simple name of the target class as the object name.
      Parameters:
      target - the target to wrap
    • SimpleErrors

      public SimpleErrors(Object target, String objectName)
      Create a new SimpleErrors holder for the given target.
      Parameters:
      target - the target to wrap
      objectName - the name of the target object for error reporting
  • Method Details

    • getObjectName

      public String getObjectName()
      Description copied from interface: Errors
      Return the name of the bound root object.
      Specified by:
      getObjectName in interface Errors
    • reject

      public void reject(String errorCode, @Nullable Object[] errorArgs, @Nullable String defaultMessage)
      Description copied from interface: Errors
      Register a global error for the entire target object, using the given error description.
      Specified by:
      reject in interface Errors
      Parameters:
      errorCode - error code, interpretable as a message key
      errorArgs - error arguments, for argument binding via MessageFormat (can be null)
      defaultMessage - fallback default message
      See Also:
    • rejectValue

      public void rejectValue(@Nullable String field, String errorCode, @Nullable Object[] errorArgs, @Nullable String defaultMessage)
      Description copied from interface: Errors
      Register a field error for the specified field of the current object (respecting the current nested path, if any), using the given error description.

      The field name may be null or empty String to indicate the current object itself rather than a field of it. This may result in a corresponding field error within the nested object graph or a global error if the current object is the top object.

      Specified by:
      rejectValue in interface Errors
      Parameters:
      field - the field name (may be null or empty String)
      errorCode - error code, interpretable as a message key
      errorArgs - error arguments, for argument binding via MessageFormat (can be null)
      defaultMessage - fallback default message
      See Also:
    • addAllErrors

      public void addAllErrors(Errors errors)
      Description copied from interface: Errors
      Add all errors from the given Errors instance to this Errors instance.

      This is a convenience method to avoid repeated reject(..) calls for merging an Errors instance into another Errors instance.

      Note that the passed-in Errors instance is supposed to refer to the same target object, or at least contain compatible errors that apply to the target object of this Errors instance.

      The default implementation throws UnsupportedOperationException since not all Errors implementations support #addAllErrors.

      Specified by:
      addAllErrors in interface Errors
      Parameters:
      errors - the Errors instance to merge in
      See Also:
    • getGlobalErrors

      public List<ObjectError> getGlobalErrors()
      Description copied from interface: Errors
      Get all global errors.
      Specified by:
      getGlobalErrors in interface Errors
      Returns:
      a list of ObjectError instances
      See Also:
    • getFieldErrors

      public List<FieldError> getFieldErrors()
      Description copied from interface: Errors
      Get all errors associated with a field.
      Specified by:
      getFieldErrors in interface Errors
      Returns:
      a List of FieldError instances
      See Also:
    • getFieldValue

      @Nullable public Object getFieldValue(String field)
      Description copied from interface: Errors
      Return the current value of the given field, either the current bean property value or a rejected update from the last binding.

      Allows for convenient access to user-specified field values, even if there were type mismatches.

      Specified by:
      getFieldValue in interface Errors
      Parameters:
      field - the field name
      Returns:
      the current value of the given field
      See Also:
    • getFieldType

      @Nullable public Class<?> getFieldType(String field)
      Description copied from interface: Errors
      Determine the type of the given field, as far as possible.

      Implementations should be able to determine the type even when the field value is null, for example from some associated descriptor.

      Specified by:
      getFieldType in interface Errors
      Parameters:
      field - the field name
      Returns:
      the type of the field, or null if not determinable
      See Also:
    • equals

      public boolean equals(@Nullable Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Description copied from interface: Errors
      Return a summary of the recorded errors, e.g. for inclusion in an exception message.
      Specified by:
      toString in interface Errors
      Overrides:
      toString in class Object
      See Also: