Published: October 11 2020
Last updated: April 13 2022

Vue 3 + VeeValidate - Required Checkbox Example (Options API)

Example built with Vue 3.2.32 and VeeValidate 4.5.11

Other versions available:

This is a quick example of how to implement a required checkbox field in Vue 3 with VeeValidate. For a more detailed registration form example that includes a bunch of other fields see Vue 3 + VeeValidate - Form Validation Example (Options API).

Vue Options API

The component in the example is built with the traditional Vue Options API that comes with Vue 2 & 3. Component logic is defined within options (e.g. components, data, methods) on an exported object.

For the same example built with the new Vue 3 Composition API see Vue 3 + VeeValidate - Required Checkbox Example (Composition API)

VeeValidate

VeeValidate is a library for building, validating and handling forms in Vue.js. VeeValidate 4 was recently released and is compatible with Vue 3, the official docs are available at https://vee-validate.logaretm.com/v4.

Here it is in action: (See on StackBlitz at https://stackblitz.com/edit/vue-3-veevalidate-required-checkbox)

Update History:

  • 13 Apr 2022 - Updated tutorial to Vue.js 3.2.32 and VeeValidate 4.5.11
  • 11 Oct 2020 - Built tutorial with Vue.js 3.0.0 and VeeValidate 4.0.0-beta.6


Vue 3 + VeeValidate Required Checkbox App Component with Options API

The app component code defines the required checkbox validation rule with the Yup schema validation library which VeeValidate supports out of the box, the checkbox is set to required with the rule Yup.bool().required() and setting the value of the checkbox input to true (:value="true"), for more info on Yup see https://github.com/jquense/yup.

The onSubmit() method is called when the form is valid and submitted, and simply displays the contents of the form in a javascript alert.

The app component template contains a form with a single required checkbox fields and a validation message. The form and checkbox field are built with the VeeValidate <Form /> and <Field /> components which automatically hook into the validation rules (schema) based on the name of the field.

The form calls the onSubmit() method when the form is submitted and valid. Validation rules are bound to the form with the validation-schema prop, and validation errors are provided to the form template via the scoped slot v-slot="{ errors }".

<script>
import { Form, Field } from 'vee-validate';
import * as Yup from 'yup';

export default {
    components: {
        Form,
        Field,
    },
    data() {
        const schema = Yup.object().shape({
            acceptTerms: Yup.bool()
                .required('Accept Ts & Cs is required')
        });

        return { 
            schema 
        }
    },
    methods: {
        onSubmit(values) {
            // display form values on success
            alert('SUCCESS!! :-)\n\n' + JSON.stringify(values, null, 4));
        }
    }
}
</script>

<template>
    <div class="card m-3">
        <h5 class="card-header">Vue 3 + VeeValidate - Required Checkbox (Options API)</h5>
        <div class="card-body">
            <Form @submit="onSubmit" :validation-schema="schema" v-slot="{ errors }">
                <div class="form-group form-check">
                    <Field name="acceptTerms" type="checkbox" id="acceptTerms" :value="true" class="form-check-input" :class="{ 'is-invalid': errors.acceptTerms }" />
                    <label for="acceptTerms" class="form-check-label">Accept Terms & Conditions</label>
                    <div class="invalid-feedback">{{errors.acceptTerms}}</div>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary mr-1">Register</button>
                    <button type="reset" class="btn btn-secondary">Reset</button>
                </div>
            </Form>
        </div>
    </div>    
</template>

 


Need Some Vue 3 Help?

Search fiverr for freelance Vue 3 developers.


Follow me for updates

On Twitter or RSS.


When I'm not coding...

Me and Tina are on a motorcycle adventure around Australia.
Come along for the ride!


Comments


Supported by