Open the-daniel-rothig opened 3 years ago
I think this is a good idea, but I think ultimately there are concerns that could come up, such as what happens in the next change after a "blur". For example, let's say you had a onChange then onblur, your onblur message appeared, then the next validation would be a change validation. Would that override the blur validation? Is it preferable to keep the blur message after the change message fires and the "light validation" doesn't return any errors? Should it continue with the "heavier validation" after the field has been touched?
I think in some ways access to field.isTouched and form.isSubmitting (or a variation that captures the validation attempt) would be preferable to the validation source, especially in the case of custom sources.
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 60 days
I would really appreciate a way to access the isSubmitting
property from the withFormik
validate
function.
Exposing the fomikBag would probably be most useful.
validate: (values, props, formikBag)=>{//do the validation here}
Feature request
Current Behavior
We have noticed that we want different validation rules to be enforced at different times. For example, in a run-of-the-mill "change password" form (current password / new password / confirm password), we could say:
!newPassword.startsWith(confirmPassword)
, we want to display a validation message. But on Blur, we want to validate a stricter rule,newPassword === confirmPassword
.So we have some rules we want to trigger onSubmit only, some rules we also want to trigger onBlur, and some rules also we want to trigger onChange. That's not currently possible to implement with Formik because the validation schema doesn't know which interaction caused the current validation run.
Desired Behavior
The
validate
function (and thevalidationSchema
'scontext
) should be provided with and indicator as to whether the validation happens in response to a change, a blur, a mount, or a submit. The implementation ofvalidate
or customyup
tests could consume this information to attain the desired behaviour.Suggested Solution
It seems to me one route to this is to add a ValidationCause enum:
and add a validationCause parameter to
validateFormWithHighPriority
,runAllValidations
,runFieldLevelValidations
,runValidationSchema
, andrunValidateHandler
,and change
runValidationSchema
andrunValidateHandler
to pass avalidationCause
context parameter / function argument in the user-providedvalidationSchema
object orvalidate
function.Who does this impact? Who is this for?
My understanding is that this is a non-breaking, additive change that will benefit users that use either form of validation (yup or a validate function). It provides advanced users with the ability to make validation feedback available in a well-timed manner.
The state of play of form validation on the web seems to be that trigger-happy or slightly-later-than-convenient validation is accepted, but I think it stands to reason that improving this would have a measurable impact on UX quality.
Describe alternatives you've considered
I tried to think about workarounds to track outside of Formik which form interaction is currently taking place (by wrapping Formik's own
onBlur
,onChange
andonSubmit
- but since I use a yup schema, ultimately I didn't know how to inject this information into my validator (I don't seem to be able to set the context through Formik). I'm considering not passing in the validationSchema into Formik at all and handle validation externally, but that seems a shame to me. I like Formik.For the "change password" example, onBlur validation is the best compromise. In design terms, we have the desire to only show the submit button when validation succeeds, but because we need the user to blur the last input to show the message, we show the submit button throughout, which is a somewhat acceptable tradeoff. Still, I think having greater nuance about when to show which validation would improve the flow
Additional context
Thanks for reading!