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.

Duplicate 'value' attribute for <input type="radio"> when using it with v-model

See original GitHub issue

The following TypeScript error is thrown:

error TS17001: JSX elements cannot have multiple attributes with the same name.
                 value="One"
                 ~~~~~

for the following template (inspired from https://vuejs.org/guide/essentials/forms.html#radio):

<template>
  <input v-model="picked" type="radio" value="One" />
  <input v-model="checked" type="checkbox" value="Two" />
</template>

<script setup lang="ts">
import { ref } from "vue"

const picked = ref([])
const checked = ref([])
</script>

Volar special-cases the <input type="checkbox"> case which works, but not the <input type="radio">:

https://github.com/johnsoncodehk/volar/blob/master/packages/vue-language-core/src/generators/template.ts#L2000

function getModelValuePropName(node: CompilerDOM.ElementNode, vueVersion: number) {
  const tag = node.tag;
  const typeAttr = node.props.find(prop => prop.type === CompilerDOM.NodeTypes.ATTRIBUTE && prop.name === 'type');
  const type = typeAttr?.value?.content;

// >>>
  if (tag === 'input' && type === 'checkbox')
    return 'checked';
// >>>>>

  if (
    tag === 'input' ||
    tag === 'textarea' ||
    tag === 'select' ||
    vueVersion < 3
  ) return 'value';

  return 'modelValue';
}

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

3reactions
LinusBorgcommented, Aug 30, 2022

Got this error when using the checkbox like this:

<input 
    type="checkbox"
    class="checkbox"
    v-model="form.show_password"
    :checked="form.show_password"
    name="show_password"
>

The ‘:checked’ is marked as the problem.

Well, this is just invalid. v-model already handles checked, so you should not add it yourself.

2reactions
melan0802commented, Aug 29, 2022

Got same error, when using v-model and value at same time like this:

<n-checkbox v-model="auth" label="root" value="root" />

vue: 2.7 volar: 0.40.2/0.40.3/0.40.4

tsconfig.json:

{
  "compilerOptions": {
    // ... other options omitted
    "allowSyntheticDefaultImports": true,
    "lib": [
      "dom",
      "esnext"
    ],
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "allowJs": true,
    "sourceMap": true,
    "noImplicitReturns": true,
    "target": "es5",
    "experimentalDecorators": true,
    "baseUrl": ".",
    "paths": {
      "@/*": [ "src/*" ]
    },
    "jsx": "preserve"
  },
  "vueCompilerOptions": {
    "target": 2.7
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.vue"
  ]
}

Temporary solution: Downgrade to 0.40.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue.js - How to get radio button value from multiple radio ...
Put the value of each radio group in the object in the array, and then calculate the selected value by computed . Share....
Read more >
Using v-model for Two-Way Binding in Vue.js | DigitalOcean
To bind the value of an input element to a property of your component's data, use v-model="dataProperty" like so.
Read more >
Form Input Bindings - Vue.js
v -model will ignore the initial value , checked or selected attributes found on any form elements. It will always treat the current...
Read more >
x-model - Alpine.js
x-model allows you to bind the value of an input element to Alpine data. Here's a simple example of using x-model to bind...
Read more >
vuejs v-for radio list and checked value - Laracasts
I have a component and I need to loop through an array of values to create a radio list (v-for is perfect) and...
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