From e345f53937c2d98bf14d278ba178e6008fd16504 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 8 Feb 2017 14:32:50 +0200 Subject: [PATCH] Document BeanValidationBinder and RequiredFieldConfigurator. (#8504) This patch also provides a documentation for #8382. Fixes #8390 --- .../datamodel/datamodel-forms.asciidoc | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/documentation/datamodel/datamodel-forms.asciidoc b/documentation/datamodel/datamodel-forms.asciidoc index 0218140425..2d1b81ea06 100644 --- a/documentation/datamodel/datamodel-forms.asciidoc +++ b/documentation/datamodel/datamodel-forms.asciidoc @@ -440,9 +440,10 @@ binder.forField(yearOfBirthField) [NOTE] Code using strings to identify properties will cause exceptions during runtime if the string contains a typo or if the name of the setter and getter methods have been changed without also updating the string. -//todo change to new JSR303 support - -Bindings created based on a property name will automatically use JSR 303 Bean Validation annotations from the bean class if a Bean Validation implementation is available. +If you have a Bean Validation implementation available in your classpath and +want to use JSR 303 Bean Validation annotations then a [classname]#BeanValidationBinder# should be used. +[classname]#BeanValidationBinder# extends [classname]#Binder# class so it has the same API but its implementation +automatically adds a bean validator which takes care of JSR 303 constraints. Constraints defined for properties in the bean will work in the same way as if configured when the binding is created. [source, java] @@ -460,8 +461,25 @@ public class Person { } ---- +[source, java] +---- +BeanValidationBinder binder = new BeanValidationBinder<>(Person.class); + +binder.bind(nameField, "name"); +binder.forField(yearOfBirthField) + .withConverter( + new StringToIntegerConverter("Please enter a number")) + .bind("yearOfBirth"); +---- + Constraint annotations can also be defined on the bean level instead of being defined for any specific property. +There are some number of predefined constraint annotations that mark a bound field as required using +[classname]#BeanValidationBinder#.[methodname]#setRequiredIndicatorVisible#. By default [classname]#@NotNull#, +[classname]#@NotEmpty# and [classname]#@Size# (if [methodname]#min()# value is greater than 0) +configures the field as required. It's possible to change this behavior using +the [classname]#BeanValidationBinder#.[methodname]#setRequiredConfigurator# method. + [NOTE] Bean level validation can only be performed once the bean has been updated. This means that this functionality can only be used together with `setBean`. You need to trigger validation manually if using `readBean` and `writeBean`. -- 2.39.5