vee-validate
vee-validate copied to clipboard
Feature: change initialValue behaviour when field wasn't touched yet
Is your feature request related to a problem? Please describe.
Current behaviour of initialValue is a bit problematic for the async use cases. I've tried to show it on this example:
https://codesandbox.io/s/basic-example-composition-api-forked-u2pc93?file=/src/App.vue:303-572
In short - even though initialValue supports being passed as a ref it doesn't seem to bring any value. After doing an API call I'm changing the the value of initialValue ref and... nothing happens. Not only the value of the field stays the same (even though I - as a user - didn't touched it yet), but also the dirty state stays unchanged.
Describe the solution you'd like
I would propose to change the way initialValue changes are being handled.
- When there was no interaction with the field whatsoever (
meta.touchedormeta.dirtyare false) the new value ofinitialValueshould be propagated directly to thevalueof the field - so there is no need for resetting the field after every API call. resetFieldshould use current value ofinitialValuewhenvaluefield is not provided.meta.dirtyshould be reevaluated every timeinitialValuechanges.
Describe alternatives you've considered
Currently I need to run resetField every time I fetch data from the API (which becomes pretty tedious when having multiple fields and multiple API calls in the same file). Also I need to pass value of initialValue every time I run resetField - it seems that resetField ignores initialValue.
https://codesandbox.io/s/basic-example-composition-api-forked-ubxk88?file=/src/App.vue:405-701
I know that I've described 3 things here, so if you believe only some of them should be introduced - please write about that. Also - I'm willing to write a PR changing this.
The behavior you are asking for is actually implemented but only works if the field is part of a form context and the initial values are controlled by the form.
https://codesandbox.io/s/basic-example-composition-api-forked-fzmz83?file=/src/App.vue
Whether or not it should be implemented on the single field level, I think it makes sense to be consistent with the form behavior and make it work similarly. I will see what I can do, if you would like to try PR then go for it! Just remember you need to do it for the useField only as useForm already has this implemented.
@logaretm Hey, thanks for the response! Ofc I'll give it a try - if you have any additional suggestions just shoot them my way. If everything goes smoothly I should file a PR this week.