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 Errors
NESTED_PATH_SEPARATOR -
Constructor Summary
ConstructorsConstructorDescriptionSimpleErrors(Object target) Create a newSimpleErrorsholder for the given target, using the simple name of the target class as the object name.SimpleErrors(Object target, String objectName) Create a newSimpleErrorsholder for the given target. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAllErrors(Errors errors) Add all errors from the givenErrorsinstance to thisErrorsinstance.booleanGet all errors associated with a field.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.inthashCode()voidRegister a global error for the entire target object, using the given error description.voidrejectValue(@Nullable String field, String errorCode, Object @Nullable [] errorArgs, @Nullable 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, for example, for inclusion in an exception message.Methods inherited from interface 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 newSimpleErrorsholder 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 newSimpleErrorsholder 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:ErrorsReturn the name of the bound root object.- Specified by:
getObjectNamein interfaceErrors
-
reject
-
rejectValue
public void rejectValue(@Nullable String field, String errorCode, Object @Nullable [] errorArgs, @Nullable String defaultMessage) Description copied from interface:ErrorsRegister 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
nullor 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:
rejectValuein interfaceErrors- Parameters:
field- the field name (may benullor 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:ErrorsAdd all errors from the givenErrorsinstance to thisErrorsinstance.This is a convenience method to avoid repeated
reject(..)calls for merging anErrorsinstance into anotherErrorsinstance.Note that the passed-in
Errorsinstance is supposed to refer to the same target object, or at least contain compatible errors that apply to the target object of thisErrorsinstance.The default implementation throws
UnsupportedOperationExceptionsince not allErrorsimplementations support#addAllErrors.- Specified by:
addAllErrorsin interfaceErrors- Parameters:
errors- theErrorsinstance to merge in- See Also:
-
getGlobalErrors
Description copied from interface:ErrorsGet all global errors.- Specified by:
getGlobalErrorsin interfaceErrors- Returns:
- a list of
ObjectErrorinstances - See Also:
-
getFieldErrors
Description copied from interface:ErrorsGet all errors associated with a field.- Specified by:
getFieldErrorsin interfaceErrors- Returns:
- a List of
FieldErrorinstances - See Also:
-
getFieldValue
Description copied from interface:ErrorsReturn 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:
getFieldValuein interfaceErrors- Parameters:
field- the field name- Returns:
- the current value of the given field
- See Also:
-
getFieldType
Description copied from interface:ErrorsDetermine 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:
getFieldTypein interfaceErrors- Parameters:
field- the field name- Returns:
- the type of the field, or
nullif not determinable - See Also:
-
equals
-
hashCode
-
toString
-