Search code examples
vue.jsbootstrap-vuevuelidate

Vuelidate check box validation


I am using Vue.js and I am new on it. I am currently working on validation. I had used vuelidate as my validation library. I had successfully done form validation, but trouble came when I had to check validation for check box.

How can I check validation for check box? Also, I had used bootstrapvue to display check box.

   <b-col lg="6" md="6" sm="6">
      <label>Bus Route</label>
      <b-form-group>
        <b-form-checkbox v-for="route in busRouteList"
                         v-model.trim="selectedRoute"
                         v-bind:value="route.value"
                         v-bind:unchecked-value="$v.selectedRoute.$touch()">
          {{route.text}}</b-form-checkbox>
      </b-form-group>
      <div class="form-group__message" v-if="$v.selectedRoute.error && !$v.selectedRoute.required">
        Field is required
      </div>
    </b-col>

validations: {

      selectedRoute: {
        required
      },
}

Solution

  • You should bind @change methods:

     <b-form-checkbox v-for="route in busRouteList"
                             v-model.trim="selectedRoute"
                             v-bind:value="route.value"
                             @change="$v.selectedRoute.$touch()">
    

    and you might want to use custom function:

    selectedRoute: {
      checked (val) {
        return val
      }
    },