Dynamic Nuxt Form Validation
Looking for some help/guidance with NuxtUI custom form validation. I've got most of my form all wired up to validation and working properly, but the "dynamic" pieces are still a mystery. I have a form section where the end user can create infinite "resources" as a new group of input fields. Each of those resources has specific validation rules for its respective form controls. Not sure how to wire this up and I'm not seeing anything in the documentation. Can anyone point me in the right direction, please?
7 Replies
Hi @deetstrab. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To address your question about dynamic form validation in NuxtUI, I can provide some guidance based on the available information in the knowledge sources.
The NuxtUI Form component supports custom validation, which can be useful for your dynamic form scenario. Here's how you can approach this:
1. Custom Validation Function:
You can use the
validate
prop on the UForm
component to implement your own validation logic. This function should return an array of errors, where each error has a message
and a path
corresponding to the form element's name attribute.
Here's an example of how you can structure your custom validation:
Custom validation (https://ui.nuxt.com/components/form#custom-validation)
2. Dynamic Form Groups:
For each dynamic resource, you can create a UFormGroup
with a unique name
attribute. This allows you to target specific fields in your validation function.
3. Handling Errors:
The Form component provides methods to handle errors dynamically. You can use these methods to set or clear errors for specific paths:
Form API (https://ui.nuxt.com/components/form#api)
While the documentation doesn't specifically cover dynamic form validation, you can leverage these features to create a custom solution for your infinite resources scenario.
Remember to adjust the validation logic as needed when resources are added or removed from your form state.
If you need more specific guidance or encounter issues implementing this approach, I recommend opening a GitHub Discussion (https://github.com/nuxt/nuxt/discussions) for the Nuxt community, as they might be able to provide more tailored assistance for your particular use case.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@108353753249222656> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
We have something similar but we use zod with the :schema prop on UForm. For the schema the property is an array and you use the z.array(). I know that’s not super helpful because you are using a custom validate function but we’ve been pretty happy with zod so I think it’s something worth to look into
So you have a
z.array(someZodValidation)
and UForm does not work with that, right?
I see. Yeah, I think it is a tough one because I don't know any such form input that returns an array of items.Sorry if I’m not being clear, using z.array does work for us.
sorry - had to step out. Yes, I was able to (with a bit of finesse), use the indexed-based naming. Thank you very much!