Skip to content
This repository was archived by the owner on Aug 10, 2023. It is now read-only.

Commit cdb1f52

Browse files
chkalivargrimstad
authored andcommitted
Updated validation chapter regarding @MvcBinding (#116)
* Updated validation chapter regarding @MvcBinding * Fixed some typos
1 parent 1882102 commit cdb1f52

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

spec/src/main/asciidoc/chapters/validation.asciidoc

+31-7
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,38 @@ Validation Exceptions
7171

7272
MVC provides an alternative exception handling mechanism that is specific for the use case described in the <<exception_mappers>> section.
7373
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.
7676

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`.
8081

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:
82106

83107
[source,java,numbered]
84108
----
@@ -151,7 +175,7 @@ created. Implementations MUST give precedence to a property (calling its getter
151175
Binding Exceptions
152176
~~~~~~~~~~~~~~~~~~
153177

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.
155179
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
156180
cannot be converted to that type.
157181

0 commit comments

Comments
 (0)