logaretm / vee-validate

✅ Painless Vue forms
https://vee-validate.logaretm.com/v4
MIT License
10.75k stars 1.26k forks source link

behaviour concerning "Paths creation and destruction" #4364

Open tbl0605 opened 1 year ago

tbl0605 commented 1 year ago

What happened?

Hi @logaretm, I don't know if I should open a bug report or a discussion, I find strange how vee-validate acts about "Paths creation and destruction" in some cases.

I have a page where a form is dynamically populated with fields, based on field descriptions extracted from JSON data. This JSON data can also dynamically change while page is open, and of course, the changes are reflected by creating/updating/deleting affected fields. I have some code that looks something like:

<Form ...>
    <Field v-for="desc in jsonFieldsDescription" :key="desc.index" :name="desc.name" :keep-value="true" v-model="models[desc.modelName]" ...>
        ...
    </Field>
</Form>

So far, so good, the most important part was that I didn't want to loose form values between JSON data changes (even when fields were gone), so I successfully used :keep-value="true".

But at the beginning of my developments I didn't add :keep-value="true" because I first didn't know about the "Paths creation and destruction" caveats. And of course, when value of desc.index changed, underlying model models[desc.modelName] was reset to undefined.

And this is where I observed a strange thing: underlying model is not reset to undefined when multiple checkboxes are bound to same name.

I made a demo to show the case: https://codesandbox.io/s/vee-validate-test-jd24vr

Click the "Generate new :key value" button and you will see all fields reset to undefined except for the "A", "B" and "C" checkboxes.

So my question is: is this expected behavior for multiple checkboxes? Or is it just a caveat? Or a bug?

Thierry.

Reproduction steps

1. 2. 3. ...

Version

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

What browsers are you seeing the problem on?

Relevant log output

No response

Demo link

https://codesandbox.io/s/vee-validate-test-jd24vr

Code of Conduct

logaretm commented 1 year ago

I don't think this is intended, but not sure if it is a bug either, I never thought about this case nor it is in any of the tests we have.

Checkboxes are pretty unique, the reason it doesn't reset them is when you change the field name it finds that the possible values are still the same as the ones bound to the current fields so it doesn't remove them from the array but that could be just a mistake. I need to dig deeper into this, thanks for reporting it and I will have an update soon.