-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Way to dynamically modify validationSchema from component child of <Formik>? #1436
Comments
Validation schema is just another prop. You can lift up some other state above your Formik component and then update it imperatively from a child component. Then you can alter the prop or return value of the function at will. |
Would be nice if EDIT: |
Hey @jaredpalmer! I'm trying out the approach you suggested by lifting state up. What I'm trying is something along the lines of this const DynamicForm = () => {
const [ schema, updateSchema ] = React.useState(intialSchema);
<Formik
validationSchema={schema}
>
<Form>
<GeneralComponents />
<CustomComponents updateSchema={updateSchema} />
</Form>
</Formik>
} I don't want the future authors of accidentally wipe out the schema, so I created this hook const useSchema = (defaultSchema) => {
const [ schema, setSchema ] = React.useState(defaultSchema);
const updateSchema = React.useCallback(
(addedSchema) => {
const newSchema = schema.concat(addedSchema);
setSchema(newSchema);
},
[ schema, setSchema ]
);
return [ schema, updateSchema ];
}; And in const CustomComponent = () => {
React.useEffect(() => {
const expressionSchema = Yup.object({
description: Yup.string(),
value: Yup.string().required('Value cannot be empty!')
});
updateSchema(expressionSchema);
}, [ updateSchema ]);
return (
// form components rendered here
)
} However this results in an infinite loop. I suspect this is because the schema reference is different each time, but I'm not sure how. For now I'm avoiding this by not passing My understanding of hooks is a bit shallow right now, but my understanding of this is since So I'm not sure what exactly I need to do in this situation 😅 Is there anyway to fix this? |
The tip from @jaredpalmer worked for me. Thank you. For reference, here is how I did it:
|
Thanks for this. Took a long time to find the solution. Hope they add to the documentation. |
It's work! I wrote examples |
In our app we allow hooks so customer can customize it and all their data has to go into the userData field. In our core app we just define it as an object and don't validate the contents, but since all their form data goes in there, need a way to update it for validation.
Here is a simplified schema
The customer will write their own components like CustomerFields below.
In CustomerFields they can use the connect() HoC to get formik props but formik.validationSchema doesn't look like something I can get the current schema and add to it. Any ideas on how I can dynamically modify the validationSchema in CustomerFields?
The text was updated successfully, but these errors were encountered: