framework icon indicating copy to clipboard operation
framework copied to clipboard

DateField ParseErrorMessage and DateOutOfRangeMessage

Open pinonpierre opened this issue 9 years ago • 6 comments

On Vaadin 8.0.6,

I don't understand how the DateField.setParseErrorMessage(String) and DateField.setDateOutOfRangeMessage(String) methods works because when I enter an invalid date, nothing change (Sometimes the field border appear in red but it seems sporadic)

Also, theses error messages are not sent to the BindingValidationStatusHandler (BindingBuilder.withValidationStatusHandler(BindingValidationStatusHandler) and StatuBindingBuilder.withStatusLabel(Label)). I think it will be a good idea to use this, no?

You can see the main problem I have in the example below, I can enter an invalid text like "abc" and when I click on "Validate!" button, the validation is correct. I would like to block the validation if the text entered into the field is incorrect and I have now way to do this especially when the field is optional.

        DateField dateField = new DateField("Date");
        dateField.setParseErrorMessage("Parse error");
        dateField.setDateOutOfRangeMessage("Out of range");
        mainLayout.addComponent(dateField);

        Label errorLabel = new Label();
        mainLayout.addComponent(errorLabel);

        Binder<Void> binder = new Binder<>();
        binder.forField(dateField)
                .withStatusLabel(errorLabel)
                .bind(o -> dateField.getEmptyValue(), null);

        Button button = new Button("Validate!");
        button.addClickListener(event1 -> {
            if (binder.validate().isOk()) {
                System.out.println("Correct");
            } else {
                System.out.println("Error!");
            }
        });
        mainLayout.addComponent(button);

Thanks,

pinonpierre avatar Jun 09 '17 13:06 pinonpierre

Same as #8687 for the error messages issue but not only because we need a way to validate the field too

pinonpierre avatar Jun 10 '17 10:06 pinonpierre

Also, if enter the wrong value right after initialization (on first selection), no error occurs(no red borders, error messages), but then choosing another value(even a right one)--> error occurs.

The code above also reproduces this issue.

anasmi avatar Jun 12 '17 05:06 anasmi

About the last comment : I think that it's coming from the changeVariables method from AbstractDateField class that is not able to see whenever the old value was an empty string (null value as LocalDate) and the new value is just unparsable (still a null value as LocalDate).

That makes me sad, really sad :'(

julio666 avatar Jun 13 '17 14:06 julio666

I took a quick look at this and seems there's a list of problems in the DateField and DateTimeField components regarding the parse errors. We are having a discussion on what to do about this and how to properly fix it.

tsuoanttila avatar Jun 14 '17 07:06 tsuoanttila

One workaround you can use for now is to validate that there is no ComponentError in the dateField: .withValidator(t -> dateField.getComponentError() == null, ctx -> ((UserError) dateField.getComponentError()).getMessage())

With the example validator above you should properly catch the parse exception. How ever the issue of weird faulty input handling (#8687) might cause some issues still.

tsuoanttila avatar Jun 14 '17 08:06 tsuoanttila

Thanks tsuoanttila for this workaround but it solves only the field validation issue and I cannot use it for now.

It doesn't solve the #8991 issue.

Unfortunately, if the user enters a bad date (i.e.: "abc") in the field, the component isn't in error (getComponentError() == null) and the validation is correct.

pinonpierre avatar Jun 14 '17 09:06 pinonpierre