An easy way to add validators
Artur- opened this issue · comments
The example at https://vaadin.com/components/vaadin-text-field/html-examples/text-field-validators-demos#synchronous-custom-validator shows you can implement your own validation in a custom component which extends text field. What I would like to do is dynamically add or remove one or more validators to a field. It seems currently doable through something like
const originalCheck = field.checkValidity;
field.checkValidity = () => {
field.invalid = false;
if (!originalCheck.call(field))
return false;
return ownChecks();
};
This is not a very intuitive way, especially that you need to reset the invalid
flag manually to false
unless the field happens to have some other validation rules enabled (failing to do so will make the the field become invalid but it will never become valid again).
Moved the issue to vaadin-core
and labelled with "next" to research for the next generation.
We can use Lion web components validate package for some inspiration.
One fundamental question is whether it's the field itself or an external "form controller" who is responsible for managing the field's validation status.
In the case of components controlled from server-side Java, the status is controlled by an external entity and any built-in validation logic is only getting in the way. The same is often also the case if you try to use 3rd party form binding libraries such as Final Form. What such solutions usually need is some way of disabling internal validation and only controlling the visual appearance (i.e. whether a required indicator is displayed and whether and error message is displayed).
Closing in favor of vaadin/web-components#81, let's continue discussion there.