如何在 vuejs2 中使用 vee-validation 为输入字段编写自定义/通用验证

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>

您可以将对象传递给您的自定义验证(甚至 return 在方法中):

<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 }
  }
}