Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upQuestion/Suggestion: "touched" changes #81
Comments
I would really like a global form "isValid" view. does anyone have an easy way to loop through formState.validity items? |
FYI - here is examlpe code to check things before submitting a form: function handleSubmit(e) {
e.preventDefault();
// make sure things were touched
const touchedValues = Object.values(formState.touched);
for (const value of touchedValues) {
if (value === false) {
setChecked(true);
return;
}
}
// check form validity
const validityValues = Object.values(formState.validity);
for (const value of validityValues) {
if (value === false) {
setChecked(true);
return;
}
}
// then dispatch a login event
dispatch({
type: 'LOGIN',
payload: formState.values,
});
} |
It will be amazing if merged, this is really needed to avoid unnecessary loops to check the whole form state. |
If think that a shorter alternative is something like that: function handleSubmit(e) {
e.preventDefault();
// make sure things were touched
const isTouched = Object.values(formState.touched).some(touched => !!touched);
const hasErrors = Object.values(formState.errors).some(error => !!error);
// check form validity
if (!isTouched || hasErrors) {
setChecked(true);
return;
}
// then dispatch a login event
dispatch({
type: 'LOGIN',
payload: formState.values,
});
} |
Any traction/response on this from the maintainers? This would be wildly helpful. |
I'd like a live comparison (as I type) of the formState to do things like enable/disable submit buttons, etc, without waiting for a blur event to trigger the actual formState.touched fields. Likewise, a global "isTouched", "isValid" (or similar), and such would be extremely handy - else this will be a super-common function that will need to be done each time.
I'd like to propose one of two options:
formState
object, or adding a 3rdextended
array element in the return. This is obviously the easier one out of the gate for me, but more fragile, less easy to keep in sync with yours, etc.That all said, I love what you've done, and the incredibly elegant interface it exposes!