You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 10, 2023. It is now read-only.
Copy file name to clipboardexpand all lines: spec/src/main/asciidoc/chapters/validation.asciidoc
+31-7
Original file line number
Diff line number
Diff line change
@@ -71,14 +71,38 @@ Validation Exceptions
71
71
72
72
MVC provides an alternative exception handling mechanism that is specific for the use case described in the <<exception_mappers>> section.
73
73
Rather than funnelling exception handling into a single location while providing no access to the bound data, controller methods may opt to act
74
-
as exception handlers as well. In other words, controller methods can get called even if parameter validation fails as long as they declare
75
-
themselves capable of handling errors.
74
+
as exception handlers as well. In other words, controller methods can get called even if parameter validation fails as long as the binding that caused
75
+
the error is defined accordingly.
76
76
77
-
A controller class that, either directly or indirectly via inheritance, defines a field or a property of type `javax.mvc.binding.BindingResult` will have its controller methods
78
-
called even if an error is encountered while validating parameters. Implementations MUST introspect the controller bean for a field or a property of this type to
79
-
determine the correct semantics; fields and property setters of this type MUST be annotated with `@Inject` to guarantee proper bean initialization [<<mvc:validation-result>>].
77
+
Parameter bindings such as `@FormParam` and `@QueryParam` may be annotated with `@MvcBinding` to enable MVC-specific binding rules. For MVC
78
+
bindings a failed validation does not result in a `ConstraintViolationException` being thrown. Instead, the corresponding `ConstraintViolation`
79
+
is stored in a request-scoped instance of `BindingResult` which can be injected into the controller. This allows the controller to handle the error
80
+
instead of relying on a global error handling mechanism like a `ExceptionMapper`.
80
81
81
-
Let us revisit the example from the <<exception_mappers>> section, this time using the controller method as an exception handler:
82
+
Let us revisit the example from the <<exception_mappers>> section, this time using `@MvcBinding` for the data binding:
83
+
84
+
[source,java,numbered]
85
+
----
86
+
public class FormDataBean {
87
+
88
+
@MvcBinding
89
+
@FormParam("age")
90
+
@Min(18)
91
+
private long age;
92
+
93
+
public long getAge() {
94
+
return age;
95
+
}
96
+
97
+
public void setAge(long age) {
98
+
this.age = age;
99
+
}
100
+
101
+
}
102
+
----
103
+
104
+
Given that the property `age` is annotated with `@MvcBinding`, the controller can act as the exception handler by handling
105
+
the error itself:
82
106
83
107
[source,java,numbered]
84
108
----
@@ -151,7 +175,7 @@ created. Implementations MUST give precedence to a property (calling its getter
151
175
Binding Exceptions
152
176
~~~~~~~~~~~~~~~~~~
153
177
154
-
As suggested by its name, instances of `BindingResult` also track any binding errors that occur while mapping request parameters to Java types.
178
+
As suggested by its name, instances of `BindingResult` also track binding errors of MVC bindings that occur while mapping request parameters to Java types.
155
179
Binding errors are discovered even before validation takes place. An example of a binding error is that of a query parameter bound to an `int` whose value
0 commit comments