Subscribe on changes!

true-value and false-value are ignored on initial render

avatar
Nov 30th 2020

Version

3.0.3

Reproduction link

https://codesandbox.io/s/hopeful-zhukovsky-yuxqi?file=/src/App.vue

Steps to reproduce

<template>
  <label>model {{model}}</label>
  <input type="checkbox" v-model="model" :true-value="false" :false-value="true" />
</template>

<script>
import {ref} from 'vue'
export default {
  name: 'App',
  setup(){
    const model=ref(false)
    return {
      model
    }
  }
}
</script>

What is expected?

checkbox expected to be checked since model is false and true-value="false"

What is actually happening?

checkbox is not checked on first render


After you click it once, it actually works okay.

avatar
Dec 1st 2020

I opened a PR,but I found that true-value and false-value are also not supported when v-model value is a Set or an Array.Is it designed like this?

avatar
Dec 1st 2020

@edison1105 yes, that's normal. With sets and arrays, there should be a value attribute instead

avatar
Dec 1st 2020

@edison1105 yes, that's normal. With sets and arrays, there should be a value attribute instead

Got it,Thanks~

avatar
Dec 1st 2020

Hi @posva I think this is not about inverted value. see https://codesandbox.io/s/fast-worker-k8q0e?file=/src/App.vue I modified the code to see the problem more clearly.

avatar
Dec 1st 2020

Because true-value or false-value is not assigned to the checkbox after first rendered.

image

avatar
Dec 2nd 2020

I think https://github.com/vuejs/vue-next/commit/48f00c0f1b574a235be40c560d2cf373be97615e is not fix this bug.

We should assign true-value or false-value to checkbox but setChecked is not do this after first render.

<div id="app">
  <label>model {{ model }}</label>
  <input type="checkbox" v-model="model" :true-value="11" :false-value="22" />
</div>

<script>
  const { ref,createApp } = Vue
  createApp({
    name: "App",
    setup() {
      const model = ref(false);
      return {
        model,
      };
    },
  }).mount('#app')
</script>

After first render,the model value is still false not 22. image

And my PR is make it works fine.

avatar
Dec 3rd 2020

Hi @posva @yyx990803 I update demo https://codesandbox.io/s/fast-worker-k8q0e?file=/src/App.vue with lastest vue-next version 3.0.4. This bug still happens.I think the model value should be 22 on initial render.

avatar
Dec 3rd 2020

model should be the source of truth, not the template. If the user intends to provide an initial value that is neither true-value or false-value, the value should be left untouched.

This actually has nothing to do with true/false-value. e.g. if you have <input type="checkbox" v-model="model"> but model's initial value is 123, it will not be changed to false.

avatar
Dec 3rd 2020

model should be the source of truth, not the template. If the user intends to provide an initial value that is neither true-value or false-value, the value should be left untouched.

This actually has nothing to do with true/false-value. e.g. if you have <input type="checkbox" v-model="model"> but model's initial value is 123, it will not be changed to false.

OK