Search code examples
vue.jsvuejs2vee-validate

How to write a custom / generic validation for the input fields using vee-validation in vuejs2


Below is my code. In that, firstname, lastname, middlename input fields are there. I want to use a singe method to throw an error for those fields. Iam able to pass only a single field inside the validator.extend function. E.g: Validator.extend('firstName', {});. But I would like to pass Validator.extend('firstName','lastName', 'middleName', {});( or )Validator.extend('fieldNames', {}); like this. Please help.

<template>
 <b-card>
    <h4 slot="header" class="card-title">Employee</h4>
        <b-row>
            <b-col sm="3">
              <b-form-group>
                <label for="name">First Name </label>
                <input type="text" id="name"  class="form-control" placeholder="Enter your name" v-validate="'required|firstName'" name="firstName">
                        <span v-show="errors.has('firstName')" class="is-danger">{{ errors.first('firstName') }}</span>
               </b-form-group>
            </b-col>
             <b-col sm="3">
              <b-form-group>
                <label for="name">Last Name </label>
                <input type="text" id="name"  class="form-control" placeholder="Enter your middle name" v-validate="'required|lastName'" name="lastName">
                        <span v-show="errors.has('lastName')" class="help is-danger">{{ errors.first('lastName') }}</span>
              </b-form-group>
            </b-col>
             <b-col sm="3">
              <b-form-group>
                <label for="name">Middle Name </label>
                <b-form-input type="text" id="name" placeholder="Enter your name"></b-form-input>
              </b-form-group>
            </b-col>
          </b-row>

          <b-row>
            <b-col sm="3">
              <b-form-group>
                <label for="name">Employee ID</label>
                <b-form-input type="text" id="name" placeholder="ID"></b-form-input>
              </b-form-group>
            </b-col>
             <b-col sm="3">
              <b-form-group>
                <label for="name">Gender</label>
                <b-form-input type="text" id="name" placeholder="Gender"></b-form-input>
              </b-form-group>
            </b-col>
             <b-col sm="3">
              <b-form-group>
                <label for="name">Nationality</label>
                <b-form-input type="text" id="name" placeholder="Nationality"></b-form-input>
              </b-form-group>
            </b-col>
          </b-row>
         <input type="submit" value="Submit" @click="validateForm">
 </b-card>
</template>

<script>

import Vue from 'vue'
import VeeValidate from 'vee-validate';
import { Validator } from 'vee-validate';
Vue.use(VeeValidate);

export default {
  name: 'addEmpl',

   created: function() {
    Validator.extend('firstName', {
    getMessage: field => 'Enter valid first name',
    validate: value => /^[a-zA-Z]*$/.test(value)
});
   },
   methods: {
    validateForm() {
        this.$validator.validateAll();
    }
   }
}
</script>

<style lang="scss" scoped>
.is-danger{
  color:  RED;
}
</style>

Solution

  • You can pass an object to your custom validation (and even return it in a method) :

    <input placeholder="Enter your name" v-validate="validations('firstName')">
    
    created() {
      Validator.extend('fieldName', {
        getMessage(field, val) {
          return `Enter valid ${val}`
        },
        validate(value, field) {
          if (field == 'firstName') { return /^[a-zA-Z]*$/.test(value) }
          else if (field == 'lastName') { return  /^[a-z]*$/.test(value) }
          // and so on
        }
      })
    },
    methods: {
      validations(field) {
        return { required: true, fieldName: field }
      }
    }