koumoul-dev / vuetify-jsonschema-form

Create beautiful and low-effort forms that output valid data. Published on npm as @koumoul/vjsf.
https://koumoul-dev.github.io/vuetify-jsonschema-form/latest/
MIT License
538 stars 154 forks source link

Defer validation #315

Open ribrewguy opened 2 years ago

ribrewguy commented 2 years ago

It appears as if the validation is triggered eagerly. This means that if I have a form of 5 required fields, they are all highlighted immediately with error text. This is not a pleasant UX because the user hasn't even begun to fill out the fields before the user is reprimanded for entering (or not entering, as the case may be) bad data.

Is there a way to defer validation until some other time (ideally a submission)?

albanm commented 2 years ago

This is not the normal behavior. The "initialValidation" option controls this. As you can see with this example the only properties with error displayed right away are those with predefined values, empty properties are only validated once the user interacted or the v-form validate method is called (click on the bottom right button). What you describe could be happening either if you use initialValidation=all or if you called validate() early, that or it is a bug with a specific schema and you can provide a codepen so that I can have a look.

ribrewguy commented 2 years ago

Thanks @albanm. I see that the initialValue option does prevent the error from displaying initially. It seems like this should be set to "none" by default, but I suppose that's a design decision not everyone would agree with.

However, I do not see a way to prevent the errors until "the v-form validate method is called". This is exactly what I'm looking for. However, I don't see how to make that work. The form always validates on input. This example codepen from the project examples demonstrates that: https://codepen.io/pen/?&editors=101.

How can the validation be completely deferred until validate is called?

albanm commented 2 years ago

Not possible right now. I agree this is a problem, I need to work some more on form validation. Unfortunately I cannot guarantee that I will be able to do so very soon.

albanm commented 2 years ago

The vuetify documentation doesn't seem to allow to do this, cf https://vuetifyjs.com/en/components/forms/#validation-with-submit-26-clear

Also the implementation doesn't seem to support it, cf https://github.com/vuetifyjs/vuetify/blob/master/packages/vuetify/src/mixins/validatable/index.ts#L148 as soon as the input has a value and was focused at least once it is susceptible to display its errors. I don't how to prevent this yet, except maybe calling resetValidation after each input which feels ugly.

This is weird, i thought i remembered that it was the default behavior. Please tell me if you think I am missing something.

albanm commented 2 years ago

The topic is discussed here: https://github.com/vuetifyjs/vuetify/issues/3530

Setting validateOnBlur and calling resetValidation feels hackish but it looks like it is the way to go.