vaadin / flow

Vaadin Flow is a Java framework binding Vaadin web components to Java. This is part of Vaadin 10+.
Apache License 2.0
608 stars 166 forks source link

Ignore validation for disabled/readonly/invisible fields #6682

Open berndklb opened 4 years ago

berndklb commented 4 years ago

Do the binder have a possibility to disable the validation for fields which are disabled / readonly or not visible?

My proposal / draft: https://github.com/vaadin/flow/compare/master...berndklb:feature/disabled_fields

Legioth commented 4 years ago

One problem with the suggested approach is how to arrange it so that the field can get validated immediately when it is no longer disabled/readonly/invisible. I'm wondering whether it would be more appropriate to instead provide a way of explicitly disabling validation (or an individual validator) for an individual binding instead of connecting it to some other state.

That kind of approach would also support cases such as if a field changes between being optional and required depending on some external factor.

heruan commented 4 years ago

I agree that disabled fields should be ignored by validation, but read-only ones shouldn't: it means the value is still important, just can't be changed.

wuttke commented 4 years ago

My workaround: binder.forField(nameField) .withValidator(v -> !nameField.isVisible() || nameField.getValue().trim().length() > 0, "....")

darmro commented 2 years ago

Hi, this enhancement would be really useful! :)

I have such a case: Validation on bean fields (jsr-303). The view can work as editable and as read-only. When read-only all fields in Binder are read-only. Going to next view a generic binder.writeBean() is called. In such a scenario writeBean should "see" that all bind fields are read-only :)

Even if I set binder.setReadOnly(true) on my form then when writeBean() is called then Vaadin calls binder.doWriteIfValid() which calls bindings.validate() on each binding and does JSR validation on bean fields. But binder is set to readOnly, so any errors cannot be corrected by the user. On the other hand binding.writeFieldValue() check this readOnly flags and does nothing when binding is readOnly. But validation is done no matter of readOnly flag.

obfischer commented 2 years ago

Same here +1

CFox17 commented 2 years ago

There's a problem with this though... Lets say you're an admin who wants to control the editability of different fields using a slider switch, and are using the "Validators.required" form control. Then lets say they clear the field's value, then slide the "editable" switch to be off, and save the changes. Then, admins would have no idea why some users were reporting issues because the field only shows as invalid if it's enabled, and it would appear to them that the field was valid...

This might be a very specific case but it is the exact case I am trying to sort out right now. There should be an option in the "Validators.required" form control to let it still apply the "required" validation even for disabled fields.