Top Description Fields Methods
org.springframework.validation

public Interface BindingResult

extends Errors
Known Direct Implementers
org.springframework.validation.AbstractBindingResult, org.springframework.validation.BindException
Imports
java.beans.PropertyEditor, java.util.Map, org.springframework.beans.PropertyEditorRegistry, org.springframework.lang.Nullable

General interface that represents binding results. Extends the Errors interface for error registration capabilities, allowing for a Validator to be applied, and adds binding-specific analysis and model building.

Serves as result holder for a DataBinder, obtained via the DataBinder#getBindingResult() method. BindingResult implementations can also be used directly, for example to invoke a Validator on it (e.g. as part of a unit test).

Author
Juergen Hoeller
Since
2.0
See Also
DataBinder, Errors, Validator, BeanPropertyBindingResult, DirectFieldBindingResult, MapBindingResult

Field Summary

Modifier and TypeField and Description
public static final String
MODEL_KEY_PREFIX

Prefix for the name of the BindingResult instance in a model, followed by the object name.

Inherited from org.springframework.validation.Errors:
NESTED_PATH_SEPARATOR

Method Summary

Modifier and TypeMethod and Description
public void
addError(ObjectError error)

Add a custom ObjectError or FieldError to the errors list.

public PropertyEditor

Returns:

the registered editor, or null if none
findEditor
(String
the path of the property (name or nested path), or null if looking for an editor for all properties of the given type
field
,
Class<?>
the type of the property (can be null if a property is given but should be specified in any case for consistency checking)
valueType
)

Find a custom property editor for the given type and property.

public Map<String, Object>
getModel()

Return a model Map for the obtained state, exposing a BindingResult instance as 'MODEL_KEY_PREFIX + objectName' and the object itself as 'objectName'.

public PropertyEditorRegistry

Returns:

the PropertyEditorRegistry, or null if none available for this BindingResult
getPropertyEditorRegistry
()

Return the underlying PropertyEditorRegistry.

public Object

Returns:

the current value of the field in its raw form, or null if not known
getRawFieldValue
(String
the field to check
field
)

Extract the raw field value for the given field.

public default String[]
getSuppressedFields()

Return the list of fields that were suppressed during the bind process.

public Object
getTarget()

Return the wrapped target object, which may be a bean, an object with public fields, a Map - depending on the concrete binding strategy.

public default void
recordFieldValue(String
the field to record the value for
field
,
Class<?>
the type of the field
type
,
Object
the original value
value
)

Record the given value for the specified field.

public default void
recordSuppressedField(String field)

Mark the specified disallowed field as suppressed.

public String[]

Returns:

the resolved message codes
resolveMessageCodes
(String
the error code to resolve into message codes
errorCode
)

Resolve the given error code into message codes.

public String[]

Returns:

the resolved message codes
resolveMessageCodes
(String
the error code to resolve into message codes
errorCode
,
String
the field to resolve message codes for
field
)

Resolve the given error code into message codes for the given field.

Inherited from org.springframework.validation.Errors:
addAllErrorsfailOnErrorgetAllErrorsgetErrorCountgetFieldErrorgetFieldErrorgetFieldErrorCountgetFieldErrorCountgetFieldErrorsgetFieldErrorsgetFieldTypegetFieldValuegetGlobalErrorgetGlobalErrorCountgetGlobalErrorsgetNestedPathgetObjectNamehasErrorshasFieldErrorshasFieldErrorshasGlobalErrorspopNestedPathpushNestedPathrejectrejectrejectrejectValuerejectValuerejectValuesetNestedPathtoString

Field Detail

MODEL_KEY_PREFIXback to summary
public static final String MODEL_KEY_PREFIX

Prefix for the name of the BindingResult instance in a model, followed by the object name.

Method Detail

addErrorback to summary
public void addError(ObjectError error)

Add a custom ObjectError or FieldError to the errors list.

Intended to be used by cooperating strategies such as BindingErrorProcessor.

See Also
ObjectError, FieldError, BindingErrorProcessor
findEditorback to summary
public PropertyEditor findEditor(String field, Class<?> valueType)

Find a custom property editor for the given type and property.

Parameters
field:String

the path of the property (name or nested path), or null if looking for an editor for all properties of the given type

valueType:Class<?>

the type of the property (can be null if a property is given but should be specified in any case for consistency checking)

Returns:PropertyEditor

the registered editor, or null if none

Annotations
@Nullable
getModelback to summary
public Map<String, Object> getModel()

Return a model Map for the obtained state, exposing a BindingResult instance as 'MODEL_KEY_PREFIX + objectName' and the object itself as 'objectName'.

Note that the Map is constructed every time you're calling this method. Adding things to the map and then re-calling this method will not work.

The attributes in the model Map returned by this method are usually included in the org.springframework.web.servlet.ModelAndView for a form view that uses Spring's bind tag in a JSP, which needs access to the BindingResult instance. Spring's pre-built form controllers will do this for you when rendering a form view. When building the ModelAndView instance yourself, you need to include the attributes from the model Map returned by this method.

See Also
getObjectName(), MODEL_KEY_PREFIX, org.springframework.web.servlet.ModelAndView, org.springframework.web.servlet.tags.BindTag
getPropertyEditorRegistryback to summary
public PropertyEditorRegistry getPropertyEditorRegistry()

Return the underlying PropertyEditorRegistry.

Returns:PropertyEditorRegistry

the PropertyEditorRegistry, or null if none available for this BindingResult

Annotations
@Nullable
getRawFieldValueback to summary
public Object getRawFieldValue(String field)

Extract the raw field value for the given field. Typically used for comparison purposes.

Parameters
field:String

the field to check

Returns:Object

the current value of the field in its raw form, or null if not known

Annotations
@Nullable
getSuppressedFieldsback to summary
public default String[] getSuppressedFields()

Return the list of fields that were suppressed during the bind process.

Can be used to determine whether any field values were targeting disallowed fields.

See Also
DataBinder#setAllowedFields
getTargetback to summary
public Object getTarget()

Return the wrapped target object, which may be a bean, an object with public fields, a Map - depending on the concrete binding strategy.

Annotations
@Nullable
recordFieldValueback to summary
public default void recordFieldValue(String field, Class<?> type, Object value)

Record the given value for the specified field.

To be used when a target object cannot be constructed, making the original field values available through getFieldValue. In case of a registered error, the rejected value will be exposed for each affected field.

Parameters
field:String

the field to record the value for

type:Class<?>

the type of the field

value:Object

the original value

Since
5.0.4
recordSuppressedFieldback to summary
public default void recordSuppressedField(String field)

Mark the specified disallowed field as suppressed.

The data binder invokes this for each field value that was detected to target a disallowed field.

See Also
DataBinder#setAllowedFields
resolveMessageCodesback to summary
public String[] resolveMessageCodes(String errorCode)

Resolve the given error code into message codes.

Calls the configured MessageCodesResolver with appropriate parameters.

Parameters
errorCode:String

the error code to resolve into message codes

Returns:String[]

the resolved message codes

resolveMessageCodesback to summary
public String[] resolveMessageCodes(String errorCode, String field)

Resolve the given error code into message codes for the given field.

Calls the configured MessageCodesResolver with appropriate parameters.

Parameters
errorCode:String

the error code to resolve into message codes

field:String

the field to resolve message codes for

Returns:String[]

the resolved message codes