TExcelTAD
Home › Vue › Vue Form Inputs

Vue Form Inputs

v-model works on every kind of form control, but the type of data you bind depends on the control. A checkbox can bind to a boolean or to an array, a radio group binds to one value, and a multi-select binds to an array. This lesson walks through each one and ends with a complete form.

Control Bind to Property & event Vue uses
<input type="text">, <textarea> string value + input
<input type="checkbox"> (single) boolean checked + change
several checkboxes array checked + change
<input type="radio"> the chosen value checked + change
<select> the chosen value value + change
<select multiple> array value + change

Checkboxes

A single checkbox binds to true/false. Several checkboxes bound to the same array collect the value of each ticked box.

html · try it yourself
<div id="app">
  <label><input type="checkbox" v-model="newsletter"> Send me the newsletter</label>
  <p>newsletter = {{ newsletter }}</p>

  <p>Toppings:</p>
  <label v-for="t in allToppings" :key="t">
    <input type="checkbox" :value="t" v-model="toppings"> {{ t }}
  </label>
  <p>toppings = {{ toppings }}</p>
</div>

<script type="module">
  import { createApp } from 'vue';

  createApp({
    data: () => ({
      newsletter: true,
      allToppings: ['cheese', 'mushroom', 'olive', 'pepper'],
      toppings: ['cheese']
    })
  }).mount('#app');
</script>

You can also choose custom values for the two states with true-value and false-value:

html
<input type="checkbox" v-model="answer" true-value="yes" false-value="no">

Radio buttons

Radio buttons bound to the same variable form a group automatically — you don't even need a name attribute.

html · try it yourself
<div id="app">
  <p>Delivery speed:</p>
  <label v-for="opt in options" :key="opt.id">
    <input type="radio" :value="opt" v-model="chosen"> {{ opt.label }} (+${{ opt.fee }})
    <br>
  </label>
  <p>Chosen: <strong>{{ chosen.label }}</strong>, fee ${{ chosen.fee }}</p>
</div>

<script type="module">
  import { createApp } from 'vue';

  const options = [
    { id: 1, label: 'Standard', fee: 0 },
    { id: 2, label: 'Express', fee: 5 },
    { id: 3, label: 'Same day', fee: 12 }
  ];

  createApp({
    data: () => ({ options, chosen: options[0] })
  }).mount('#app');
</script>

Binding :value (instead of the plain value attribute) lets you use any JavaScript value — here a whole object.

Select boxes

html · try it yourself
<div id="app">
  <select v-model="country">
    <option disabled value="">Please choose a country</option>
    <option v-for="c in countries" :key="c.code" :value="c.code">{{ c.name }}</option>
  </select>
  <p>country = "{{ country }}"</p>

  <p>Languages (hold Ctrl / ⌘ to pick several):</p>
  <select v-model="langs" multiple size="4">
    <option>JavaScript</option>
    <option>Python</option>
    <option>Rust</option>
    <option>Go</option>
  </select>
  <p>langs = {{ langs }}</p>
</div>

<script type="module">
  import { createApp } from 'vue';

  createApp({
    data: () => ({
      country: '',
      countries: [
        { code: 'VN', name: 'Vietnam' },
        { code: 'JP', name: 'Japan' },
        { code: 'DE', name: 'Germany' }
      ],
      langs: ['JavaScript']
    })
  }).mount('#app');
</script>

Tip: Start a single select with an empty, disabled option whose value is "", and initialise the model to ''. Otherwise iOS Safari shows the first option as selected but never fires change for it.

When an <option> has no value, its text is used as the value, as in the languages list.

A complete form

Putting it together: a registration form with validation messages, a disabled submit button and @submit.prevent.

html · try it yourself
<style>
  form { display: grid; gap: 8px; max-width: 280px; font-family: sans-serif; }
  .error { color: #b91c1c; font-size: 13px; }
  pre { background: #f4f4f4; padding: 8px; }
</style>

<div id="app">
  <form @submit.prevent="register">
    <label>Name <input v-model.trim="form.name"></label>
    <span class="error" v-if="form.name.length < 2">At least 2 characters</span>

    <label>Age <input type="number" v-model.number="form.age" min="1"></label>

    <label>Plan
      <select v-model="form.plan">
        <option value="free">Free</option>
        <option value="pro">Pro</option>
      </select>
    </label>

    <label><input type="checkbox" v-model="form.terms"> I accept the terms</label>

    <button :disabled="!valid">Register</button>
  </form>
  <pre>{{ form }}</pre>
</div>

<script type="module">
  import { createApp } from 'vue';

  createApp({
    data: () => ({
      form: { name: '', age: 18, plan: 'free', terms: false }
    }),
    computed: {
      valid() {
        return this.form.name.length >= 2 && this.form.age > 0 && this.form.terms;
      }
    },
    methods: {
      register() {
        console.log('Registering', JSON.stringify(this.form));
      }
    }
  }).mount('#app');
</script>

{{ form }} prints the whole object as JSON, which is a handy debugging trick. valid is a computed property — covered in detail soon.

Note: Grouping all fields in one form object makes it easy to reset (this.form = { …defaults }) or send to a server in one piece.

Test yourself

Several checkboxes share v-model="selected". What type should selected be?

  1. A boolean
  2. A string
  3. An array
  4. A number

Why use :value instead of value on a radio button?

  1. value is not allowed on radios
  2. :value lets the value be any JavaScript value, such as a number or object
  3. :value makes it checked by default
  4. There is no difference

What does v-model bind to on a <select multiple>?

  1. An array of the selected values
  2. The first selected value
  3. A comma-separated string
  4. The selected index