In the docs it is said …
The validation state of each field is updated whenever the user modifies the value of that field.
So if i have a validator registered in a textfield wich is binded to a property I can see the error message after i wrote the name field with less than 4 characters.
[font]
Binder<Person> binder = new Binder<>();
TexField name = new TextField();
binder.forField(name)
.withValidator( n -> n.length() < 4,
"name must have at least 4 characters")
.bind(Person::getName, Person::setName)
[/font]
But if i use bean validation i can’t see the same.
@Size(min = 4, message = "name must have at least 4 characters")
String name
BeanValidationBinder<Person> binder = new BeanvalidationBinder<>(Person.class);
binder.bindInstanceFields(form);
I thougth that it was the same because BeabValidationBinder extends Binder.
So in that case when does it happen the validation?
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.0.Final</version>
</dependency>
I believed that it was enough.
So, Java Validation API is a specification (that’s the
validation-
api dependency). You need also an implementation. Two popular implementations are the ones provided by Apache (the dependencies I posted), and Hibernate (the ones Tatu posted). Let us know if it works!
It doesn´t work, something escape me!!!
I have in the POM the two dependencies (api and implementation of bean validation)
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.5.Final</version>
</dependency>
What is the moment in wich the validation trigger the error message?
After the user update the field (that it was happening with .withvalidator.
In the doc it is said …
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. I am not sure what bean level means. My annotation is in the property name of the bean person.
“Bean level validation” are constraints at the class level. So I guess, it’s not your case (you have a field-level constraint: @Size). With Hibernate Validator you also need this if you are not deploying your app in a JEE container:
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.1-b08</version>
</dependency>
If that doesn’t work, are you able to share the complete
pom.xml file and relevant Java classes?
Now it works but i was surprised about the * in the fields with annotation. It appears first * (like required) and ! (when the user begin to write the three first characters).
What is the meaning of this *? It isnt required beacause the only annotation was @Size.
It should work for all of them. Did you configure a “null representation”? Also, double check you imported the correct @NotNull annotation (
javax
.validation.constraints.NotNull ).