Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<title>Vee-validate V4 - lazy validation not working - rule is being called on each keystroke #4737

Closed
1 of 5 tasks
davidkjackson54 opened this issue Apr 27, 2024 · 4 comments

Comments

@davidkjackson54
Copy link

What happened?

I have a rule that I want to execute only after the user has entered all the information into the text field and tabbed away. I execute an async call in that rule.
I cannot get this to work as the rule is being called on each key stroke.
My Field definition looks like this:

div>
        <Field name="PackageModel" type="text" as="vscode-text-field" v-model = packageCreateParms.PackageModel 
              :rules="asyncCall" :title=constants.copyForwardText> Package Model
        </Field>
        <ErrorMessage name="PackageModel" class="error"></ErrorMessage> 
</div>

I have tried inserting
:validateOnBlur="true" :validateOnChange="true" :validateOnInput="false" :validateOnModelUpdate="false" into the Field definition and various combinations of those 4 parameters but it doesn't make any difference as the rule continues to be called on each keystroke.
Would appreciate any advice as to why I am doing wrong here.

Reproduction steps

See above code fragment

Version

Vue.js 3.x and vee-validate 4.x

What browsers are you seeing the problem on?

  • Firefox
  • Chrome
  • Safari
  • Microsoft Edge

Relevant log output

None

Demo link

None

Code of Conduct

@logaretm
Copy link
Owner

This was reported a lot of times and is intended.

Validation being lazy means the errors are generated lazily, but the validation still occurs. This is because the meta.valid flag must be correct, there is no way around it in terms of whate vee-validate can do. Either we make the valid flag incorrect till a validation is executed or we run rules implicitly like we do now.

This behavior existed in v2, v3 where the valid flag could've a value of null to indicate that it never ran any validations yet, but people confused it with false and rightfully so, it is a falsy value. And it caused a lot of issues to be reported. So it's either I satisfy this camp or that camp. No in between.

This means there is no solution for it, but there might be workarounds you can employ:

  • Caching the input value within the validator so if the user types it again for the same input, it won't execute the expensive request.
  • Empty values usually are falsy for this kind of validation, right? you can skip running it all together if the value is empty within the validator logic.
  • You can debounce the network requests ensure only one validation is run every 300 ms or so, you can further chain the validation results promises to return the value of the last validation run. Many techniques exist for async debouncing.

Unfortunately its all user-land, any changes here means a breaking change and that's reserved for v5.

@davidkjackson54
Copy link
Author

davidkjackson54 commented Apr 28, 2024 via email

@davidkjackson54
Copy link
Author

davidkjackson54 commented Apr 28, 2024 via email

@davidkjackson54
Copy link
Author

davidkjackson54 commented Apr 28, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants