enthusiastic-js / form-observer

A simple utility for reacting to events from a form's fields
MIT License
7 stars 0 forks source link

Potential Feature: Add a `revalidateOn` Option to the `FormValidityObserver` #8

Closed ITenthusiasm closed 5 months ago

ITenthusiasm commented 5 months ago

Motivation

Most form validation libraries will provide developers with options for revalidating a field that has already been validated at least once.

How It Works

Consider a situation where a developer has asked a form validation utility to validate fields onfocusout (the bubbling version of onblur) and to revalidate them oninput. Let's say that an email field is in the form and is required. If a user visits the field and then leaves it (focusout) without entering anything, then the field will be marked as invalid, and an error message like "Email is required" will be displayed.

The user realizes that they cannot ignore the field. So they return to it and start entering a value. Because this field was already validated at least once, the revalidation feature kicks in. In our case, revalidation occurs oninput; so as the user starts typing in a value, the error message gets updated. If the user enters "e", then the error message will immediately change to "Email is invalid" because the field was revalidated oninput. This error will continue to be displayed until the user provides a valid email address. Note that because revalidation is now happening oninput, the user will be able to see that their field is correct before they leave it (i.e., "blur" it). Note that validation will still occur onfocusout. However, this won't cause a noticeable difference anymore because the field will have been validated on its last input event before the field is blurred.

Sometime later, the user might choose to be silly and modify the email field to something invalid again. As they do so, the error messages will reemerge oninput. Remember: Because this field was already validated at least once, the revalidation feature is active. So even if the user makes the field valid, it will still be validated oninput from this point onwards.

Now let's say that the user has moved on to the confirm-email field. They've learned their lesson with the email field and don't have any interest in generating any error messages. They start to type the letter e; but because the field has not yet been validated (automatically or manually), no error message pops up. They perfectly supply a valid email with a value that matches what's seen in the other email field. So when they leave the confirm-email field, no error message pops up.

This means that the user was able to interact with the confirm-email field without ever seeing an error message for it. However, once the user leaves the field, it will be validated (because the focusout event will have triggered). Consequently, if the user returns to the field and makes it invalid, then they will see error messages for that field.

Difficulty of Implementation

Moderate

The effort certainly shouldn't be significant. But the effort isn't trivial either. (The effort might be "easy", if that were a value between "trivial" and "moderate".)

Usage

From the standpoint of a developer, the usage would be simple and would look something like the following:

const observer = new FormValidityObserver("focusout", { revalidateOn: "input" });

Other Notes

const observer = new FormValidityObserver("input", { revalidateOn: "input" });

This would be wasteful and redundant. Therefore, it should be forbidden. (We won't go as far as throwing errors at runtime, though. That's also arguably a waste, though not a significant one.)

ITenthusiasm commented 5 months ago

Resolved by #9.