question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useField mutates parent modelValue

See original GitHub issue

What 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>

image

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:closed
  • Created a year ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
zkulbedacommented, Aug 5, 2022

Hi! For updating v-model variable, Vue 3 expects update:modelValue (docs). That’s why the first warn appears. Correct this line:

update:model-value update:modelValue’: (v) => typeof v === ‘boolean’,

For preventing vee-validate from emitting update:modelValue, use syncVModel option:

const email = useField('email', undefined, {
  syncVModel: false
}).value;

Full example: https://stackblitz.com/edit/vue-72nwzl?file=src/components/SomePopup.vue

1reaction
logaretmcommented, Oct 19, 2022

However, I believe this was a dangerous breaking change for a point release.

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found