vee-validate icon indicating copy to clipboard operation
vee-validate copied to clipboard

Feature: change initialValue behaviour when field wasn't touched yet

Open FRSgit opened this issue 3 years ago • 2 comments

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.

  1. When there was no interaction with the field whatsoever (meta.touched or meta.dirty are false) the new value of initialValue should be propagated directly to the value of the field - so there is no need for resetting the field after every API call.
  2. resetField should use current value of initialValue when value field is not provided.
  3. meta.dirty should be reevaluated every time initialValue changes.

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.

FRSgit avatar Jul 15 '22 10:07 FRSgit

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 avatar Jul 16 '22 02:07 logaretm

@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.

FRSgit avatar Jul 17 '22 22:07 FRSgit