I’m working on a React Native project where I’m using React Hook Form for a dynamic form, along with Valibot for validation. I have a scenario where I need to hide some input fields based on certain conditions. However, these fields are also required under specific conditions.
I’ve tried using register and unregister to handle this, but it doesn’t seem to work as expected. When I hide the input, I don’t want it to be validated, but when it’s visible, it should be required.
Can anyone guide me on how to properly manage conditional inputs with validation in React Hook Form while using Valibot? Any help would be appreciated.
2
Answers
I use zod for validation in my nextjs project. I had similar issue on my project where selection of week of the day should be array of numbers when type is Subscription and null when type is oneTime. Here is my approach.
Here the data through form can be accessed in the refine method, and custom validation can be done. Maybe there are similar methods in valibot too(check method as of the documentation)
For valibot this should work
If user pick supplier as "JS" the ecmaVersion field will get required
My solution to the problem was supported by the maintainer