Vaadin DateField validation does not show validati

2019-08-02 23:44发布

问题:

Hello I'm using Vaadin 8 and im trying to use Vaadins DateField for user input.

private DateField date = new DateField("Date of Birth");
...
binder.forField(date).asRequired("Some Warning").withValidator(new DateValidator()).bind(Person::getDateOfBirth, Person::setDateOfBirth);

The DateValidator checks if the Person is at least 18 years old.

The Problem is that if I use the Datepicker, that is integrated in the DateField no validation error is shown to the user if the Person is younger than 18. But when I type in the date and hit enter or switch to another input field the validation error appears.

How can I achieve that the validation error is shown when the input is given via the Datepicker?

回答1:

Apparently Validator doesn't manage ValueChange event. You can do it on your own like this:

date.addValueChangeListener( event -> 
        validate( event.getSource().getDefaultValidator(), event.getValue() ) );

And some example validate() function:

private void validate(Validator validator, LocalDateTime dateTime)
{
    validator.apply(dateTime, ...);
}