Rethinking the "per-event" validation approach #487
-
Currently, it is possible to define different validation rules in different events (onChange, onBlur, onSubmitAsync etc...). If I'm seeing potential issues with this approach (separating out validation per event - onChange vs onBlur vs onMount etc...). I don't know if there is a use case where users would want to do different validations in onChange and onBlur and onSubmitAsync for example. From my experience, a field is either valid or invalid at a given time, regardless of when the validation is done, and users may often end up reusing the same validation rules in onChange and onSubmitAsync or in onBlur and onSubmitAsync for example. Imo, it could make things simpler if we decoupled the validation rules (what makes a field valid) from when the validation should be done. Another challenge created by this approach is that errors are cleared by the same event that raised them. For example if onBlur returns a validation error, that error will only be cleared the next time the field is blurred. In some use cases, users could want to do the initial validation on blur but remove the errors as the user is typing to fix them. Here is an example: (type 2 characters in the first name field then focus the other field: "First name must be at least 3 characters" is shown. It could be good to be able to remove the error as soon as the user types the third character). I believe this is also what is causing this issue about validating untouched forms. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
I like this idea. It’s safe to assume that there is a cascading matrix configuration for this. Eg, when you do an onchange validation, you also want it to automatically do an on submit as well. The sync/async distinction is still important. We should figure this out quickly.
Tanner Linsley
…On Oct 26, 2023 at 7:29 AM -0700, Julien Delort ***@***.***>, wrote:
Currently, it is possible to define different validation rules in different events (onChange, onBlur, onSubmitAsync etc...). If onChange does some validation, that validation is only run when the onChange callback is triggered (at each keystroke). So if the user submits the form without touching it, the validation is not done (onSubmitAsync needs to be implemented in thiscase).
I'm seeing potential issues with this approach (separating out validation per event - onChange vs onBlur vs onMount etc...). I don't know if there is a use case where users would want to do different validations in onChange and onBlur and onSubmitAsync for example. From my experience, a field is either valid or invalid at a given time, regardless of when the validation is done, and users may often end up reusing the same validation rules in onChange and onSubmitAsync or in onBlur and onSubmitAsync for example.
Imo, it could make things simpler if we decoupled the validation rules (what makes a field valid) from when the validation should be done.
Another challenge created by this approach is that errors are cleared by the same event that raised them. For example if onBlur returns a validation error, that error will only be cleared the next time the field is blurred. In some use cases, users could want to do the initial validation on blur but remove the errors as the user is typing to fix them. Here is an example: (type 2 characters in the first name field then focus the other field: "First name must be at least 3 characters" is shown. It could be good to be able to remove the error as soon as the user types the third character).
I believe this is also what is causing this issue about validating untouched forms.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Thanks Tanner! I have a suggestion regarding the validation API: at the field level we could replace the
We could also expose similar options at the form level which would apply to all fields (unless some fields override it). The only difference would be Any thoughts? |
Beta Was this translation helpful? Give feedback.
-
While I've responded to the suggested API change, allow me to propose my own:
|
Beta Was this translation helpful? Give feedback.
-
No worries, thanks for the feedback! |
Beta Was this translation helpful? Give feedback.
Yes. This is pretty much what I was getting at. Also @crutchcorn, we discussed scoping the validators so as to not interfere with potential semantics around a generic field onChange functionality (which may or may not also be something we add in the future)