flow icon indicating copy to clipboard operation
flow copied to clipboard

Ignore validation for disabled/readonly/invisible fields

Open berndklb opened this issue 4 years ago • 5 comments

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

  • Use-Case: Legacy Software, where a field was not mandatory in the past, but now the field is required. So if we open an old view in readOnly mode this field will be empty. If the user tries do navigate for example to the next subview, the validation for the fields is failing.

  • Use-Case: If we have several input fields which depends on for example a CheckBox. If the CheckBox is checked, the input fields have to be filled. Otherwise the input fields can be empty. So the UI Design could be, if checkBox is checked disable the input fields. So the user knows, that no input is required. It would be also possible to make the input fields readOnly or not visible.

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

berndklb avatar Oct 15 '19 07:10 berndklb

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.

Legioth avatar Oct 15 '19 09:10 Legioth

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.

heruan avatar Oct 15 '19 09:10 heruan

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

wuttke avatar Apr 16 '20 20:04 wuttke

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.

darmro avatar Mar 25 '22 09:03 darmro

Same here +1

obfischer avatar Jul 20 '22 16:07 obfischer

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.

CFox17 avatar Aug 18 '22 22:08 CFox17