Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntegerField / AbstractNumberField: Validation for Min/Max has no message / prevents custom validation #6055

Open
hoshie82 opened this issue Feb 16, 2024 · 4 comments
Labels

Comments

@hoshie82
Copy link

Description

If you set a range for a number field and enter an invalid value, there is no error message shown, because the validation result returns an empty error message.. It is not possible to do a custom validation because that validation is not triggered, after the default validator has failed.

Expected outcome

Either the default validator should be triggered after all custom validators have been fired, or the validation result should contain all errors...
Or: custom error messages for Min/Max should be possible for the number fields.

Minimal reproducible example

        VerticalLayout layout = new VerticalLayout();
        IntegerField field = new IntegerField();
        field.setMin(50); // for this test, other values than custom validator
        field.setMax(250);
        Button button = new Button("validate");
        layout.add(field, button);
        Binder<TestObject> binder = new Binder<>();
        binder.forField(field)
                .withValidator((v, ctx) -> {
                    if (v == null) {
                        return ValidationResult.ok();
                    }
                    if (v < 100 || v > 200) {
                        return ValidationResult.error("not valid");
                    }
                    return ValidationResult.ok();
                })
                .bind(TestObject::getValue, TestObject::setValue);

        button.addClickListener(e -> {
            BinderValidationStatus<TestObject> state = binder.validate();
            if (state.hasErrors()) {
                String msg = state.getValidationErrors().stream()
                        .map(ValidationResult::getErrorMessage).collect(Collectors.joining("\n"));
                Notification.show(msg);
            }
        });

Steps to reproduce

if you enter the value 40, nothing is shown, because the default validator of the integer field is triggered. The custom validator does nothing.
If you enter the value 50, the "not valid" message is shown, because the custom validator is triggered.

Environment

Vaadin version(s): 24.3.5
OS: win10

Browsers

Issue is not browser related

@knoobie
Copy link
Contributor

knoobie commented Feb 16, 2024

See #4618

@yuriy-fix
Copy link
Contributor

In the latest version you are also able to skip default validator: vaadin/flow#17178.

@yuriy-fix yuriy-fix added enhancement New feature or request Impact: High labels Feb 22, 2024
@hoshie82
Copy link
Author

@yuriy-fix
problem is, that I don't want to skip all default validators at the binder, because "some" of the validators make sense... Skipping this validator would be better for my use case, because I only use the min/max attributes at client side.

@knoobie
adding additional messages would be great. But in my oppinion the custom validation should be also preferred against the default-validator, if only one error message is shown.

Same problem with "asRequired()" at the binder, but at least you can add a custom message there.

@knoobie
Copy link
Contributor

knoobie commented Feb 22, 2024

The binder works by "stopping" at the first error; therefore the default validation of the field (with no error message) is triggered before your validation runs, so this kind of precedence is currently not there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants