Open yekver opened 4 years ago
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
ping
That's reasonable. The only issue I see with it is that some people may regenerate the validationSchema on every render, causing validation on every render, which might cause a state update and thus an infinite loop.
I think we only apply a new errors
object if it isn't the same as the old, so the infinite loop condition may not occur.
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
up
As a workaround, I've mounted this component inside the form:
const RevalidateOnSchemaChange = ({ validationSchema }) => {
const { validateForm } = useFormikContext();
useEffect(() => {
validateForm();
}, [validationSchema, validateForm]);
return null;
};
Could be a custom hook etc, but I wanted it inside the Formik
render callback
Just ran into this myself. Great workaround @lexanth
@lexanth or @rgallison how do you actually update the validationSchema to formik? How do you send the new schema?
@fjbotto You just change whatever you are passing into the validation
For example, the following is a pretty common pattern:
const [isDraft, setIsDraft] = useState(true)
let schema = isDraft ? schemaA : schemaB;
const handleSubmit= () => setIsDraft(false);
return <Formik onSubmit={handleSubmit} validationSchema={schema}>...</Formik>
🐛 Bug report
Current Behavior
right now if
validationSchema
prop changed form wouldn't revalidateExpected behavior
form should run validation on every
validationSchema
prop changeYour environment