nuxt / ui

A UI Library for Modern Web Apps, powered by Vue & Tailwind CSS.
https://ui.nuxt.com
MIT License
3.79k stars 463 forks source link

Custom Form Validation #1992

Open moshetanzer opened 1 month ago

moshetanzer commented 1 month ago

Environment


Version

latest

Reproduction

https://stackblitz.com/edit/nuxt-ui-jnwh5u?file=app.vue

Description

Hi @benjamincanac

Hope you well.

Not sure if this is a bug, but when using an if else in custom form validation even though errors are cleared from errors[] doesnt clear ui until you re-click submit.

Additional context

No response

Logs

No response

moshetanzer commented 1 month ago

@romhml any clues?

romhml commented 1 month ago

Not sure I understand the issue here, errors are updated after interacting with the inputs in the reproduction you provided. Errors are updated on submit or input events. What behaviour are you expecting?

moshetanzer commented 1 month ago

Hey @romhml,

Issue I am facing is that only the single select that was interacted with is being updated on input even though in the validate function if all are equal to zero I want the message to be updated for all of them.

Basically some validation occurs on input, but it only occurs for interacted field - there are cases (like when using zod's refine or superRefine) that you have a set of 3 inputs that should be considered "as one validation rule" so that user gets correct feedback for those inputs (like in this use case that if it is they all are zero they all should have a validation message before submit).

Does that make more sense?

romhml commented 1 month ago

This is expected, we only update errors for the input that has been interacted with. If you want to tie the validation for these three inputs you have to group them under the same FormGroup element like so: https://stackblitz.com/edit/nuxt-ui-gbdc1x?file=app.vue

moshetanzer commented 1 month ago

Hey @romhml,

Thanks for this, do get the logic - though think that it should work even not in the same form group since then you are restricted to the ui form group definition for all inputs that are part of a single validation rule. For example how would you set a different label for each input when using this?

romhml commented 1 month ago

It won't be possible to set different label with the example I gave you. Another work around would be to use form.setErrors in the validate function to update the error of related inputs.

moshetanzer commented 1 month ago

Hey @romhml

Yip. That is exactly what I have done @change to custom clear and setErrors.

Don't you think this should be fixed? Think using refine for eg in Zod or or in joi is super common and makes it a pain to use this form component?