useField mutates parent modelValue
See original GitHub issueWhat happened?
When i use form in nested component with modelValue property, useForm
, bound to <input />
mutates parent component modelValue on <input />
@update:modelValue
event.
Reproduction steps
npm i vee-validate@4.6.2
App.vue
<template>
<div id="app">
<SomePopup v-model="visible" />
visible = {{ visible }}
</div>
</template>
<script>
import SomePopup from './components/SomePopup.vue';
import { ref } from 'vue';
export default {
name: 'App',
components: { SomePopup },
setup() {
const visible = ref(true);
return {
visible,
};
},
};
</script>
SomePopup.vue
<template>
<div v-if="modelValue" id="popup">
<input name="email" v-model="email" />
</div>
</template>
<script>
import { useField } from 'vee-validate';
export default {
props: {
modelValue: {
type: Boolean,
},
},
emits: {
'update:model-value': (v) => typeof v === 'boolean',
},
setup() {
const email = useField('email').value;
return {
email,
};
},
};
</script>
Version
Vue.js 3.x and vee-validate 4.x
What browsers are you seeing the problem on?
- Firefox
- Chrome
- Safari
- Microsoft Edge
Relevant log output
[Vue warn]: Component emitted event "update:modelValue" but it is neither declared in the emits option nor as an "onUpdate:modelValue" prop.
at <App>
[Vue warn]: Invalid prop: type check failed for prop "modelValue". Expected Boolean, got String with value "sadsad".
at <SomePopup
modelValue="sadsad"
onUpdate:modelValue=fn
>
### Demo link
https://vue-bvzvxv.stackblitz.io
### Code of Conduct
- [X] I agree to follow this project's [Code of Conduct](CONDUCT.md)
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
logaretm/vee-validate - set field value programmatically - GitHub
until I change the modelValue in parent component, modelValue in input component change correctly, but the field value does not update,
Read more >vue.js - V-Model is not Reactive (Vue3) - Stack Overflow
With this TextInput component: value does not need to be a prop; Just emit the update:modelValue event to the parent (this should be...
Read more >Update parent state using update:modelValue in Composition ...
Learn how to update the parent state from child component using the update:modelValue event emitter in the Composition API in Vue.js with ...
Read more >Validate Forms using vee-validate with Vue3 Composition API
Every change made on the input is automatically assigned to the form object that we will define on the parent. The useField hook...
Read more >Using v-model on Nested Vue Components - Zaengle Corp
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi! For updating v-model variable, Vue 3 expects
update:modelValue
(docs). That’s why the first warn appears. Correct this line:‘
update:model-valueupdate:modelValue’: (v) => typeof v === ‘boolean’,For preventing vee-validate from emitting
update:modelValue
, usesyncVModel
option:Full example: https://stackblitz.com/edit/vue-72nwzl?file=src/components/SomePopup.vue
I agree. In hindsight, this should’ve been turned off by default. I reviewed the download numbers on v4 releases and docs metrics and rationalized it could be safe enough to sneak in because it would be easily missable otherwise especially since I did get a lot of issues requesting it.
Safe to say this is too late to revert now, but I will be more careful of such changes in the future. Thanks for your feedback!