How to enable build-in DateField validation?

Hi all,

If
DateField
is set a date format, it checks that the user input value corresponds to this format. However the validation message is not displayed unless
setImmediate(true)
is set.

My question is: how to benefit from date format checker in case when
immediate=false
(I would like to perform a form validation on form submit)? Right now I see that component error property is set for date field, but never displayed on form submission (date gets
null
value).

My positive validation rule is: either the date is empty (null), or the date corresponds to date format.

Thanks for any input.

You should have the DateField immediate if you want the (server-side) validation to take place when the user selects a value or leaves the field. Note that you can still have the form write-through mode false, so that the value is only written to an internal buffer on the server until the form is committed. If the field is not immediate, validation takes place when there is an immediate event from the UI - either form submission (explicit validation) or other similar event.

The client (browser) first tries to parse the date based on the used locale, and the method DateField.handleUnparsableDateString() is called on the server if the client failed to parse the date. This is separate from Vaadin validators, and called before any of them. By default (in the latest Vaadin versions), the method throws an exception, which results in the value being set as null. You might want to override that parsing method if you use a custom date format.


Henri
, thank you a lot for your remarks!

I want to implement server-side validation, but when a complete form is submitted. Unfortunately, I failed to do so:

  1. The first time I entered the correct value using the popup date picker. Form is submitted, everything is fine.
  2. I have altered the date field, so it is not parseable (2010-11-aa). The field is marked with a error tip but the form is submitted with the previous (valid value). Is it a bug?

Regarding 2., this looks like a bug, although the mechanism is a bit strange. Made a report (
#5927
.

You might be able to work around the problem by reimplementing the bad isValid() method for the DateField component, or something similar.