remix-validated-form icon indicating copy to clipboard operation
remix-validated-form copied to clipboard

[Feature]: Better dependent field validation UX & DX

Open airjp73 opened this issue 2 years ago • 1 comments

What is the new or updated feature that you are suggesting?

Based off of #172.

When the validity of a field depends on the value of another field, the behavior of the validation isn't quite right.

The example by @jnicklas is this:

yup.object().shape({
  foo: yup.boolean(),
  bar: yup.string().when('foo', { is: true, then: (s) => s.required() }),
});

If the field bar has an error and the user unchecks the checkbox foo, then the error on bar doesn't go away.

Possible solution

The current solution I have in mind is something like this:

  • Validate the entire form whenever validate is called
  • Clear any errors that are no longer present
  • When adding errors validation, look at the touched state and validationBehavior config for each field and use that information to determine if an error should be present.

This would constitute a breaking change, so we would make this change for v5.

I still want to do some thinking about this change before moving ahead with it, though. I've been tossing around other ideas for potential API changes for v5 that might change this idea as well.

Why should this feature be included?

Currently, to get the desired behavior, the user must manually call validate for the dependent field. Any other modern form library like Formik or react-hook-form would handle this behavior automatically.

airjp73 avatar Oct 01 '22 18:10 airjp73

I just got bit by this one. Maybe add this to the FAQ in README until it is supported, it may help some library users save a bit of time.

Would you accept a PR to improve on this ?

andreasrs avatar Oct 28 '22 11:10 andreasrs

@andreasrs, I would definitely accept a PR for this (a docs PR or the fix itself). Happy to provide guidance if you wanted to pick it up.

airjp73 avatar Nov 06 '22 14:11 airjp73

v5 needs to happen soonish to address #261, so I'm hoping to get this addressed soon.

airjp73 avatar Mar 22 '23 14:03 airjp73

Fixed in 00f69b8191ddfebf10bb6c69e92f1baf2ee0cf3e, hoping to release sometime this week.

airjp73 avatar Jun 07 '23 16:06 airjp73