-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Fields don't clean up their rules properly #4643
Labels
🐛 bug
Unintended behavior
Comments
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. |
bgoscinski
pushed a commit
to bgoscinski/vee-validate
that referenced
this issue
Jan 29, 2024
logaretm
pushed a commit
that referenced
this issue
Jan 29, 2024
closes #4643 Co-authored-by: Bartosz Gościński <bartosz.goscinski@gmail.com>
logaretm
pushed a commit
that referenced
this issue
Jan 29, 2024
closes #4643 Co-authored-by: Bartosz Gościński <bartosz.goscinski@gmail.com>
This was referenced Jun 26, 2024
This was referenced Sep 12, 2024
This was referenced Sep 20, 2024
This was referenced Oct 25, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 linevee-validate/packages/vee-validate/src/useForm.ts
Line 562 in b96155c
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
The text was updated successfully, but these errors were encountered: