vee-validate插件的使用

使用说明

npm install [email protected] 项目目录里头安装

当前页面上的大部分教程使用中文包的时候会出错,或者报方法不存在,未定义等,是因为版本的问题

validate.js 范本

import Vue from 'vue'
import VeeValidate, { Validator } from 'vee-validate'
import zh from 'vee-validate/dist/locale/zh_CN'// 引入中文文件
// 配置中文
Validator.addLocale(zh)
const config = {
  errorBagName: 'errors', // change if property conflicts.
  fieldsBagName: 'fieldBags',  // 报冲突时 可自定义修改字段名称
  delay: 0, // 错误提示的延迟时间
  strict: true, // 没有设置规则的表单不进行校验,
  enableAutoClasses: false,
  locale: 'zh_CN', // 对语言(中文)的配置
  classNames: {
    touched: 'touched', // the control has been blurred
    untouched: 'untouched', // the control hasn't been blurred
    valid: 'valid', // model is valid
    invalid: 'invalid', // model is invalid
    pristine: 'pristine', // control has not been interacted with
    dirty: 'dirty' // control has been interacted with
  },
  events: 'input', //* *input|blur** 在用户输入和表单失去焦点时都进行校验 可单独写  blur或input
  inject: true
}
Vue.use(VeeValidate, config)

// 自定义validate
const dictionary = {
  zh_CN: {
    messages: {
      email: () => '请输入正确的邮箱格式',
      required: (field) => '请输入' + field
    },
    attributes: {
      email: '邮箱',
      password: '密码',
      task_name: '任务名称',
      phone: '手机',
      task_type: '任务类型',
      task_template: '任务模板',
      task_tine_type: '时间类型',
      task_tine_value: '时间类型值',
      assisting_department: '协助单位',
      responsible_department: '责任单位',
      target_area: '目标新增面积',
      target_cahnge_number: '目标改造数量',
      implementation_plan: '实施方案',
      assessment_standard: '考核指标',
      grading_standard: '评分标准',
      score: '考核分值'
    }
  }
}

Validator.updateDictionary(dictionary)

Validator.extend('phone', {
  messages: {
    zh_CN: field => field + '必须是11位手机号码'
  },
  validate: value => {
    return value.length === 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/.test(value)
  }
})

使用案例

前端页面代码


    
        
        {{ errors.first('task_name') }}
    

表单提交进行统一验证

async validateForm () {
  let AdditionalColumnList = this.AdditionalColumnList
  let arr = {
    task_name: this.form.task.name,
    task_type: this.form.task.type,
    task_template: this.form.template_id,
    task_tine_type: this.form.fields.time.type,
    task_tine_value: this.form.fields.time.value,
    assisting_department: this.form.fields.assisting_department,
    responsible_department: this.form.fields.responsible_department,
    target_area: this.form.fields.target_area,
    target_cahnge_number: this.form.fields.target_cahnge_number,
    implementation_plan: this.form.fields.implementation_plan,
    assessment_standard: this.form.assessment_standard,
    grading_standard: this.form.grading_standard,
    score: this.form.score
  }
  let that = this
  AdditionalColumnList.forEach(function (item) {
    arr[item.name] = that.form.fields.additional_fields[item.code]
  })
  let res = await this.$validator.validateAll(arr)
  return res
},

备注

this.validator.validateAll(arr) 检验所有的包含里头的字段,当所有字段都验证通过时 返回true

你可能感兴趣的:(vee-validate插件的使用)