Closed bgoscinski closed 8 months ago
In the meantime I created a very hacky workaround:
import { Field, FormContextKey } from 'vee-validate';
import { getCurrentInstance, inject, onBeforeUnmount } from 'vue';
const { setup } = Field;
Field.setup = function wrappedSetup(props, ctx) {
onBeforeUnmount(() => {
const form = inject(FormContextKey, undefined);
if (!form) return;
const { id } = getCurrentInstance().exposed.meta;
const states = form.getAllPathStates();
const idx = states.findIndex(
(state) => (Array.isArray(state.id) ? state.id.includes(id) : state.id === id),
);
if (idx >= 0) {
// Move the correct pathState to the beginning of the states array
// so that vee-validate's removePathState finds it first
states.unshift(states.splice(idx, 1)[0]);
}
});
return setup.call(this, props, ctx);
};
Stackblitz with the workaround applied: https://stackblitz.com/edit/vee-validate-v4-checkboxes-1zts45?file=src%2Fmain.ts
Thanks for reporting this, I took a quick look and seems like it was caused by a couple of assumptions we had:
feel free to PR it. we would need a test to accompany it if possible.
What happened?
I created a field using the composition API (
useField
), and I have a template that conditionally renders a different version of it (a<Field>
with the same path, but different validation rules). When I change the condition in such a way that causes the<Field>
to be unmounted then vee-validate still respects the<Field>
's rules.I dug a bit into the vee-validate internals and found out that
useField
registersonBeforeUnmount
that should cleanup the relevantpathState
from the form but theremovePathState
finds and removes a pathState with differentid
than the one created by<Field>
. I think that this line https://github.com/logaretm/vee-validate/blob/b96155c5a558cb3d219e0c8a93b4db36a9bf9f40/packages/vee-validate/src/useForm.ts#L562 should have been written more like this:I can prepare a PR with this change if you think this is the right way to fix it
Reproduction steps
<Field>
which specifiesrules=required
is rendered.<Field>
to be unmounted and should result in removal of thepathState
that specifies the required rule.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://stackblitz.com/edit/vee-validate-v4-checkboxes-t1tqgz?file=src%2FApp.vue
Code of Conduct