Repository navigation
setFieldValue shouldValidate validates whole form instead of the given field #4015
Replies: 1 comment
|
Yeah, that is the expected behavior. When you pass The reason The trick is to wait for the state update to resolve. Since // Update without validating immediately, then validate specifically
setFieldValue('foo', 'bar', false).then(() => {
validateField('foo');
});However, the most idiomatic Formik way is to just let the full validation run but only show the error once the field is marked as // Standard approach: validate everything but control UI with 'touched'
setFieldValue('foo', 'bar', true);
setFieldTouched('foo', true, false);
// UI
{errors.foo && touched.foo && <div>{errors.foo}</div>}This keeps your code simple and avoids fighting Formik's internal reconciliation. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I'm calling
setFieldValuewithshouldValidate(docs):And it seems to be validating the whole form instead of just
foofield, is this the expected behavior?As a workaround I tried using
validateField:But
validateFielddoesn't have acess to the updated value offoo, so it won't work.Any suggestions? I'm using
v2.2.10withvalidationSchema.All reactions