Package org.springframework.validation

# 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:
  - `Validator.validateObject(Object)`
  - `BeanPropertyBindingResult`
  - `DirectFieldBindingResult`
  - Serialized Form

## Field Summary

### Fields inherited from interface org.springframework.validation.Errors

`NESTED_PATH_SEPARATOR`

## Constructor Summary

Constructors
Constructor
Description
`SimpleErrors(Object target)`
Create a new `SimpleErrors` holder for the given target, using the simple name of the target class as the object name.
`SimpleErrors(Object target, String objectName)`
Create a new `SimpleErrors` holder for the given target.

## Method Summary

Modifier and Type
Method
Description
`void`
`addAllErrors(Errors errors)`
Add all errors from the given `Errors` instance to this `Errors` instance.
`boolean`
`equals(Object other)`
`List<FieldError>`
`getFieldErrors()`
Get all errors associated with a field.
`Class<?>`
`getFieldType(String field)`
Determine the type of the given field, as far as possible.
`Object`
`getFieldValue(String field)`
Return the current value of the given field, either the current bean property value or a rejected update from the last binding.
`List<ObjectError>`
`getGlobalErrors()`
Get all global errors.
`String`
`getObjectName()`
Return the name of the bound root object.
`int`
`hashCode()`
`void`
`reject(String errorCode, Object[] errorArgs, String defaultMessage)`
Register a global error for the entire target object, using the given error description.
`void`
`rejectValue(String field, String errorCode, Object[] errorArgs, String defaultMessage)`
Register a field error for the specified field of the current object (respecting the current nested path, if any), using the given error description.
`String`
`toString()`
Return a summary of the recorded errors, for example, for inclusion in an exception message.

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.springframework.validation.Errors

`failOnError, getAllErrors, getErrorCount, getFieldError, getFieldError, getFieldErrorCount, getFieldErrorCount, getFieldErrors, getGlobalError, getGlobalErrorCount, getNestedPath, hasErrors, hasFieldErrors, hasFieldErrors, hasGlobalErrors, popNestedPath, pushNestedPath, reject, reject, rejectValue, rejectValue, setNestedPath`

## 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:
  - `Errors.rejectValue(String, String, Object[], String)`

### 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:
  - `Errors.reject(String, Object[], String)`

### 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:
  - `Errors.getAllErrors()`

### 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:
  - `Errors.getFieldErrors()`

### 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:
  - `Errors.getGlobalErrors()`

### 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:
  - `Errors.getFieldType(String)`

### 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:
  - `Errors.getFieldValue(String)`

### 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, for example, for inclusion in an exception message.

Specified by:
  `toString` in interface `Errors`
Overrides:
  `toString` in class `Object`
See Also:
  - `Errors.failOnError(Function)`