vaadin / flow-components

Java counterpart of Vaadin Web Components
99 stars 66 forks source link

Validation refactor to fix issues related to the validated event #5537

Open vursen opened 11 months ago

vursen commented 11 months ago

Motivation

There are several problems with the current approach based on the validated event:

  1. Server-side validation is triggered on blur even if nothing has been typed (#17429, #6146)
  2. Server-side validation is triggered too frequently, resulting in performance degradation (#4390).
  3. Server-side validation is triggered on client property changes, for example resulting in unexpected Binder revalidation after a bean reset (#5399, #5319, #5330, #5328).
  4. Server-side validation is triggered inconsistently for text fields: fields validate on change when valid but on input when invalid (#4986).

All these problems have emerged after the introduction of the validated event, so we need to find an alternative. We should remember that the underlying purpose of the validated event was to trigger server-side validation when the user enters bad input, as there is no change event in that case.

Solution

For components that allow entering bad input, let’s replace the validated event with a new event unparsable-change:

However, switching to the new event will address (4) only partially. The validation will be still inconsistent in eager mode: text fields will validate on input when entering valid values but on change when entering bad input. To improve this, let’s additionally subscribe the text field Flow components to hasInputValue property change and trigger validation when it occurs in eager mode.

For the rest components, let's remove the validated event so that they will only validate on change event.

Affected components

Note DateTimePicker has more complex UX, which requires a more complex solution to achieve consistent validation behavior. There will be another proposal for it later.

Alternatives

Listen directly to the blur event in the Flow component and trigger validation when it’s fired.

Pros:

Cons:

Additional context

Related Github discussion: https://github.com/orgs/vaadin/discussions/4434

Related AC that introduced the validated event: https://github.com/vaadin/platform/issues/3066

TatuLund commented 11 months ago
  1. Server-side validation is triggered on blur even if nothing has been typed

Note, also client side validation is triggered on blur even if nothing has been typed as seen here: https://github.com/vaadin/web-components/issues/6587

vursen commented 11 months ago

Note, also client side validation is triggered on blur even if nothing has been typed as seen here: https://github.com/vaadin/web-components/issues/6587

I'd like to clarify that this proposal focuses only on Flow components. It's specifically designed in a way that avoids modifying any existing behavior of the web components, such as validation on blur, for example. If you believe that the web components shouldn't validate on blur, feel free to create a separate ticket.

vursen commented 11 months ago

This refactor is planned to be delivered by 24.3-beta.

yuriy-fix commented 9 months ago

The refactor has covered all input components apart from DateTimePicker which will have a separate refactor planned.