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

a way to programatically set form/$model value without triggering validators #1227

Open
renatodeleao opened this issue Feb 23, 2024 · 0 comments

Comments

@renatodeleao
Copy link

renatodeleao commented Feb 23, 2024

Problem

If we want to implement a form "reset to defaults values", there's no way to do it without making all validators run on previous value set — either via $model or the reactive form mutation. This is particularly useful if you have async validators that do any sort of API requests. Resetting to a previous value will still trigger the useless API request and currently there's no easy way to prevent it without manually fiddle/hacking your configs.

Solution

The first one that comes to my mind is imitate vee-validate. With the current api, make $reset method accept a value argument and if that value is present (!== undefined) do not run any validators when field model is set, aside from marking the field $dirty as false.

Reference: see #resetField chapter

The second one would be a new method $slientSetModel our $silentModel computed setter that performs the same thing.

Alternative

I've noticed that is possible to achieve this if we activate $lazy on the useVuelidate hook level (it doesn't work on validator level config), and then do a combination of $reset() plus mutate the reactive form directly (not the $model). In my case, activating this breaks a lot of specs so I would have to patch a lot of things just to get something that could be available without any config limitation.

Demo

Additional context

This should be possible to do because $rewardEarly does exactly that. It prevents validators to run until $commit is called. In my case I cannot set this validation mode.

Final thoughts

Would be nice to have a way to do it regardless of the config, resetting values programatically is fairly common form scenario.

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

1 participant