Top Description Fields Constructors Methods
org.springframework.validation

public Class SimpleErrors

extends Object
implements Errors, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.springframework.validation.Errors
Annotations
@SuppressWarnings:serial
Imports
java.beans.PropertyDescriptor, java.io.Serializable, java.lang.reflect.Field, java.util.ArrayList, .List, org.springframework.beans.BeanUtils, org.springframework.lang.Nullable, org.springframework.util.Assert, .ObjectUtils, .ReflectionUtils, .StringUtils

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.

Author
Juergen Hoeller
Since
6.1
See Also
Validator#validateObject(Object), BeanPropertyBindingResult, DirectFieldBindingResult

Field Summary

Modifier and TypeField and Description
private final List<FieldError>
private final List<ObjectError>
private final String
private final Object

Constructor Summary

AccessConstructor and Description
public
SimpleErrors(Object
the target to wrap
target
)

Create a new SimpleErrors holder for the given target, using the simple name of the target class as the object name.

public
SimpleErrors(Object
the target to wrap
target
,
String
the name of the target object for error reporting
objectName
)

Create a new SimpleErrors holder for the given target.

Method Summary

Modifier and TypeMethod and Description
public void
addAllErrors(Errors
the Errors instance to merge in
errors
)

Overrides default org.springframework.validation.Errors.addAllErrors.

Add all errors from the given Errors instance to this Errors instance.

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public List<FieldError>
getFieldErrors()

Implements org.springframework.validation.Errors.getFieldErrors.

Get all errors associated with a field.

public Class<?>
getFieldType(String
the field name
field
)

Overrides default org.springframework.validation.Errors.getFieldType.

Determine the type of the given field, as far as possible.

public Object
getFieldValue(String
the field name
field
)

Implements org.springframework.validation.Errors.getFieldValue.

Return the current value of the given field, either the current bean property value or a rejected update from the last binding.

public List<ObjectError>
public String
getObjectName()

Implements org.springframework.validation.Errors.getObjectName.

Return the name of the bound root object.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public void
reject(String
error code, interpretable as a message key
errorCode
,
Object[]
error arguments, for argument binding via MessageFormat (can be null)
errorArgs
,
String
fallback default message
defaultMessage
)

Implements org.springframework.validation.Errors.reject.

Register a global error for the entire target object, using the given error description.

public void
rejectValue(String
the field name (may be null or empty String)
field
,
String
error code, interpretable as a message key
errorCode
,
Object[]
error arguments, for argument binding via MessageFormat (can be null)
errorArgs
,
String
fallback default message
defaultMessage
)

Implements org.springframework.validation.Errors.rejectValue.

Register a field error for the specified field of the current object (respecting the current nested path, if any), using the given error description.

public String
toString()

Overrides java.lang.Object.toString.

Implements org.springframework.validation.Errors.toString.

Return a summary of the recorded errors, e.g. for inclusion in an exception message.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

fieldErrorsback to summary
private final List<FieldError> fieldErrors
globalErrorsback to summary
private final List<ObjectError> globalErrors
objectNameback to summary
private final String objectName
targetback to summary
private final Object target

Constructor Detail

SimpleErrorsback to summary
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:Object

the target to wrap

SimpleErrorsback to summary
public SimpleErrors(Object target, String objectName)

Create a new SimpleErrors holder for the given target.

Parameters
target:Object

the target to wrap

objectName:String

the name of the target object for error reporting

Method Detail

addAllErrorsback to summary
public void addAllErrors(Errors errors)

Overrides default org.springframework.validation.Errors.addAllErrors.

Doc from org.springframework.validation.Errors.addAllErrors.

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.

Parameters
errors:Errors

the Errors instance to merge in

Annotations
@Override
equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getFieldErrorsback to summary
public List<FieldError> getFieldErrors()

Implements org.springframework.validation.Errors.getFieldErrors.

Doc from org.springframework.validation.Errors.getFieldErrors.

Get all errors associated with a field.

Returns:List<FieldError>

a List of FieldError instances

Annotations
@Override
getFieldTypeback to summary
public Class<?> getFieldType(String field)

Overrides default org.springframework.validation.Errors.getFieldType.

Doc from org.springframework.validation.Errors.getFieldType.

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.

Parameters
field:String

the field name

Returns:Class<?>

the type of the field, or null if not determinable

Annotations
@Override
getFieldValueback to summary
public Object getFieldValue(String field)

Implements org.springframework.validation.Errors.getFieldValue.

Doc from org.springframework.validation.Errors.getFieldValue.

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.

Parameters
field:String

the field name

Returns:Object

the current value of the given field

Annotations
@Override
@Nullable
getGlobalErrorsback to summary
public List<ObjectError> getGlobalErrors()

Implements org.springframework.validation.Errors.getGlobalErrors.

Doc from org.springframework.validation.Errors.getGlobalErrors.

Get all global errors.

Returns:List<ObjectError>

a list of ObjectError instances

Annotations
@Override
getObjectNameback to summary
public String getObjectName()

Implements org.springframework.validation.Errors.getObjectName.

Doc from org.springframework.validation.Errors.getObjectName.

Return the name of the bound root object.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
rejectback to summary
public void reject(String errorCode, Object[] errorArgs, String defaultMessage)

Implements org.springframework.validation.Errors.reject.

Doc from org.springframework.validation.Errors.reject.

Register a global error for the entire target object, using the given error description.

Parameters
errorCode:String

error code, interpretable as a message key

errorArgs:Object[]

error arguments, for argument binding via MessageFormat (can be null)

defaultMessage:String

fallback default message

Annotations
@Override
rejectValueback to summary
public void rejectValue(String field, String errorCode, Object[] errorArgs, String defaultMessage)

Implements org.springframework.validation.Errors.rejectValue.

Doc from org.springframework.validation.Errors.rejectValue.

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.

Parameters
field:String

the field name (may be null or empty String)

errorCode:String

error code, interpretable as a message key

errorArgs:Object[]

error arguments, for argument binding via MessageFormat (can be null)

defaultMessage:String

fallback default message

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Implements org.springframework.validation.Errors.toString.

Doc from org.springframework.validation.Errors.toString.

Return a summary of the recorded errors, e.g. for inclusion in an exception message.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

Annotations
@Override