Class SimpleErrors
- All Implemented Interfaces:
Serializable
,Errors
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:
-
Field Summary
Fields inherited from interface org.springframework.validation.Errors
NESTED_PATH_SEPARATOR
-
Constructor Summary
ConstructorDescriptionSimpleErrors
(Object target) Create a newSimpleErrors
holder for the given target, using the simple name of the target class as the object name.SimpleErrors
(Object target, String objectName) Create a newSimpleErrors
holder for the given target. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAllErrors
(Errors errors) Add all errors from the givenErrors
instance to thisErrors
instance.boolean
Get all errors associated with a field.Class<?>
getFieldType
(String field) Determine the type of the given field, as far as possible.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.Get all global errors.Return the name of the bound root object.int
hashCode()
void
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.toString()
Return a summary of the recorded errors, e.g.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
Create a newSimpleErrors
holder for the given target, using the simple name of the target class as the object name.- Parameters:
target
- the target to wrap
-
SimpleErrors
Create a newSimpleErrors
holder for the given target.- Parameters:
target
- the target to wrapobjectName
- the name of the target object for error reporting
-
-
Method Details
-
getObjectName
Description copied from interface:Errors
Return the name of the bound root object.- Specified by:
getObjectName
in interfaceErrors
-
reject
Description copied from interface:Errors
Register a global error for the entire target object, using the given error description. -
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 interfaceErrors
- Parameters:
field
- the field name (may benull
or empty String)errorCode
- error code, interpretable as a message keyerrorArgs
- error arguments, for argument binding via MessageFormat (can benull
)defaultMessage
- fallback default message- See Also:
-
addAllErrors
Description copied from interface:Errors
Add all errors from the givenErrors
instance to thisErrors
instance.This is a convenience method to avoid repeated
reject(..)
calls for merging anErrors
instance into anotherErrors
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 thisErrors
instance.The default implementation throws
UnsupportedOperationException
since not allErrors
implementations support#addAllErrors
.- Specified by:
addAllErrors
in interfaceErrors
- Parameters:
errors
- theErrors
instance to merge in- See Also:
-
getGlobalErrors
Description copied from interface:Errors
Get all global errors.- Specified by:
getGlobalErrors
in interfaceErrors
- Returns:
- a list of
ObjectError
instances - See Also:
-
getFieldErrors
Description copied from interface:Errors
Get all errors associated with a field.- Specified by:
getFieldErrors
in interfaceErrors
- Returns:
- a List of
FieldError
instances - See Also:
-
getFieldValue
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 interfaceErrors
- Parameters:
field
- the field name- Returns:
- the current value of the given field
- See Also:
-
getFieldType
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 interfaceErrors
- Parameters:
field
- the field name- Returns:
- the type of the field, or
null
if not determinable - See Also:
-
equals
-
hashCode
public int hashCode() -
toString
Description copied from interface:Errors
Return a summary of the recorded errors, e.g. for inclusion in an exception message.
-