001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.validation;
018
019import java.beans.PropertyEditor;
020import java.util.Map;
021
022import org.springframework.beans.PropertyEditorRegistry;
023
024/**
025 * General interface that represents binding results. Extends the
026 * {@link Errors interface} for error registration capabilities,
027 * allowing for a {@link Validator} to be applied, and adds
028 * binding-specific analysis and model building.
029 *
030 * <p>Serves as result holder for a {@link DataBinder}, obtained via
031 * the {@link DataBinder#getBindingResult()} method. BindingResult
032 * implementations can also be used directly, for example to invoke
033 * a {@link Validator} on it (e.g. as part of a unit test).
034 *
035 * @author Juergen Hoeller
036 * @since 2.0
037 * @see DataBinder
038 * @see Errors
039 * @see Validator
040 * @see BeanPropertyBindingResult
041 * @see DirectFieldBindingResult
042 * @see MapBindingResult
043 */
044public interface BindingResult extends Errors {
045
046        /**
047         * Prefix for the name of the BindingResult instance in a model,
048         * followed by the object name.
049         */
050        String MODEL_KEY_PREFIX = BindingResult.class.getName() + ".";
051
052
053        /**
054         * Return the wrapped target object, which may be a bean, an object with
055         * public fields, a Map - depending on the concrete binding strategy.
056         */
057        Object getTarget();
058
059        /**
060         * Return a model Map for the obtained state, exposing a BindingResult
061         * instance as '{@link #MODEL_KEY_PREFIX MODEL_KEY_PREFIX} + objectName'
062         * and the object itself as 'objectName'.
063         * <p>Note that the Map is constructed every time you're calling this method.
064         * Adding things to the map and then re-calling this method will not work.
065         * <p>The attributes in the model Map returned by this method are usually
066         * included in the {@link org.springframework.web.servlet.ModelAndView}
067         * for a form view that uses Spring's {@code bind} tag in a JSP,
068         * which needs access to the BindingResult instance. Spring's pre-built
069         * form controllers will do this for you when rendering a form view.
070         * When building the ModelAndView instance yourself, you need to include
071         * the attributes from the model Map returned by this method.
072         * @see #getObjectName()
073         * @see #MODEL_KEY_PREFIX
074         * @see org.springframework.web.servlet.ModelAndView
075         * @see org.springframework.web.servlet.tags.BindTag
076         */
077        Map<String, Object> getModel();
078
079        /**
080         * Extract the raw field value for the given field.
081         * Typically used for comparison purposes.
082         * @param field the field to check
083         * @return the current value of the field in its raw form, or {@code null} if not known
084         */
085        Object getRawFieldValue(String field);
086
087        /**
088         * Find a custom property editor for the given type and property.
089         * @param field the path of the property (name or nested path), or
090         * {@code null} if looking for an editor for all properties of the given type
091         * @param valueType the type of the property (can be {@code null} if a property
092         * is given but should be specified in any case for consistency checking)
093         * @return the registered editor, or {@code null} if none
094         */
095        PropertyEditor findEditor(String field, Class<?> valueType);
096
097        /**
098         * Return the underlying PropertyEditorRegistry.
099         * @return the PropertyEditorRegistry, or {@code null} if none
100         * available for this BindingResult
101         */
102        PropertyEditorRegistry getPropertyEditorRegistry();
103
104        /**
105         * Add a custom {@link ObjectError} or {@link FieldError} to the errors list.
106         * <p>Intended to be used by cooperating strategies such as {@link BindingErrorProcessor}.
107         * @see ObjectError
108         * @see FieldError
109         * @see BindingErrorProcessor
110         */
111        void addError(ObjectError error);
112
113        /**
114         * Resolve the given error code into message codes.
115         * <p>Calls the configured {@link MessageCodesResolver} with appropriate parameters.
116         * @param errorCode the error code to resolve into message codes
117         * @return the resolved message codes
118         */
119        String[] resolveMessageCodes(String errorCode);
120
121        /**
122         * Resolve the given error code into message codes for the given field.
123         * <p>Calls the configured {@link MessageCodesResolver} with appropriate parameters.
124         * @param errorCode the error code to resolve into message codes
125         * @param field the field to resolve message codes for
126         * @return the resolved message codes
127         */
128        String[] resolveMessageCodes(String errorCode, String field);
129
130        /**
131         * Mark the specified disallowed field as suppressed.
132         * <p>The data binder invokes this for each field value that was
133         * detected to target a disallowed field.
134         * @see DataBinder#setAllowedFields
135         */
136        void recordSuppressedField(String field);
137
138        /**
139         * Return the list of fields that were suppressed during the bind process.
140         * <p>Can be used to determine whether any field values were targeting
141         * disallowed fields.
142         * @see DataBinder#setAllowedFields
143         */
144        String[] getSuppressedFields();
145
146}