Handling Nested dynamic fields - one place for all duplicate issues like this, hopefully 😅 #2476
-
Need some advice! My use case scenario 😬The form fields in my form are dynamic in the sense that the number of fields inside the formik's How I plan to solve this 🚀Now, to validate this I have the precisely clear two ways -
I plan to keep field level validations because, this way, my fields (are of 12 types right now) individually know their own validations. My form does not worry about dynamic additions to it because the new entries render with their validations. What I also considered 😇But, since I ❤️ using yup validation schema with formik, I already did a POC to generate a dynamic validation schema for the fields and it works. Because the next requested feature (rendering dependent fields) involves dynamically changing the fields, I'll have to maintain the schema in the state of my component so that I can dynamically update this when the fields (that will also be a part of component's state) update. What do you think 🤔Is it a good idea to use yup in formik when my validation schema is continuously updating every time a prop/state variable is updating on which the schema is dependent? P.S. If the title seems like a clickbait, then, trust me, I have gone through a lot of click baits while searching on google, StackOverflow, github, Medium blogs... 😛 Edit: Previously the title of this discussion was Dynamic validation schema - it's not what it seems |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
I think I am very interested in your solution ! :) |
Beta Was this translation helpful? Give feedback.
-
Hey! So this is a great question. There is no silver bullet here. If you find it easier to use field-level validation, then do it. It can be nice to colocate validation where you make the decision about what to render. It’s really useful when fields are added / removed like the situation you described, but also rely on some kind interim state or side effect that is not accessible to Formik’s validate or validationSchema’s function scopes. Another approach is to write a function that returns a validation function or validation schema. This works well when you can derive/predict all validation from just the values and state/props above your form (and guarantee that no keys are going to be added that you can’t predict ahead of time). If you have dynamic stuff that is dependent of some state/effect that you can’t somehow lift above Formik or predict the validation of, then form-level validation won’t easily work for that. This is rare though. Luckily, even if you do find yourself in that situation, you can also combine form-level and field-level validation in the same form. Just be aware that the results will be deeply merged. Hope that helps! |
Beta Was this translation helpful? Give feedback.
Hey! So this is a great question. There is no silver bullet here.
If you find it easier to use field-level validation, then do it. It can be nice to colocate validation where you make the decision about what to render. It’s really useful when fields are added / removed like the situation you described, but also rely on some kind interim state or side effect that is not accessible to Formik’s validate or validationSchema’s function scopes.
Another approach is to write a function that returns a validation function or validation schema. This works well when you can derive/predict all validation from just the values and state/props above your form (and guarantee that no keys are going to be a…