官网:https://baianat.github.io/vee-validate/
一、安装
1 | npm install vee-validate --save |
直接安装会报错:
__WEBPACK_IMPORTED_MODULE_2_vee_validate__.a.addLocale is not a function
vee-validate的版本问题,回退到2.0.0-rc.25就可以了。可以先卸载npm uninstall vee-validate,
然后安装旧版版本 npm install vee-validate@2.0.0-rc.25
1 | npm install vee-validate@2.0.0-rc.25 |
二、引用
1 2 3 | import Vue from 'vue' ; import VeeValidate from 'vee-validate' ; Vue.use(VeeValidate); |
组件设置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import VeeValidate, { Validator } from 'vee-validate' ; import messages from 'assets/js/zh_CN' ; Validator.updateDictionary({ zh_CN: { messages } }); const config = { errorBagName: 'errors' , // change if property conflicts. delay: 0, locale: 'zh_CN' , messages: null , strict: true }; Vue.use(VeeValidate,config); |
assets/js/zh_CN 代表你存放语言包的目录,从node_modules/vee-validate/dist/locale目录下面拷贝到你的项目
Validator还有更多应用,下面再讲。
config其它参数,delay代表输入多少ms之后进行校验,messages代表自定义校验信息,strict=true代表没有设置规则的表单不进行校验,errorBagName属于高级应用,自定义errors,待研究
三、基础使用
1 2 3 4 5 6 7 | <div class = "column is-12" > <label class = "label" for = "email" >Email</label> <p class = "control" > <input v-validate data-rules= "required|email" : class = "{'input': true, 'is-danger': errors.has('email') }" name= "email" type= "text" placeholder= "Email" > <span v-show= "errors.has('email')" class = "help is-danger" >{{ errors.first( 'email' ) }}</span> </p> </div> |
提醒:错误信息里面的名称通常就是表单的name属性,除非是通过Vue实例传递进来的。
提醒:养成好习惯,给每个field添加name
属性,如果没有name
属性又没有对值进行绑定的话,validator可能不会对其进行正确的校验
关于errors:
上面的代码我们看到有errors.has
,errors.first
,errors是组件内置的一个数据模型,用来存储和处理错误信息,可以调用以下方法:
errors.first('field')
- 获取关于当前field的第一个错误信息collect('field')
- 获取关于当前field的所有错误信息(list)has('field')
- 当前filed是否有错误(true/false)all()
- 当前表单所有错误(list)any()
- 当前表单是否有任何错误(true/false)add(String field, String msg)
- 添加错误clear()
- 清除错误count()
- 错误数量remove(String field)
- 清除指定filed的所有错误
关于Validator
Validator是以$validator
被组件自动注入到Vue实例的。同时也可以独立的进行调用,用来手动检查表单是否合法,以传入一个对象的方式,遍历其中指定的field。
1 2 3 4 5 6 | import { Validator } from 'vee-validate' ; const validator = new Validator({ email: 'required|email' , name: 'required|alpha|min:3' , }); // or Validator.create({ ... }); |
你也可以在构造了validator之后设置对象参数
1 2 3 4 5 6 7 | import { Validator } from 'vee-validate' ; const validator = new Validator(); validator.attach( 'email' , 'required|email' ); // attach field. validator.attach( 'name' , 'required|alpha' , 'Full Name' ); // attach field with display name for error generation. validator.detach( 'email' ); // you can also detach fields. |
最后你也可以直接传值给field,测试是否可以通过校验,像这样:
1 2 3 4 5 6 7 8 | validator.validate( 'email' , 'foo@bar.com' ); // true validator.validate( 'email' , 'foo@bar' ); // false //或者同时校验多个: validator.validateAll({ email: 'foo@bar.com' , name: 'John Snow' }); //只要有一个校验失败了,就返回false |
四、内置的校验规则
after{target}
- 比target要大的一个合法日期,格式(DD/MM/YYYY)alpha
- 只包含英文字符alpha_dash
- 可以包含英文、数字、下划线、破折号alpha_num
- 可以包含英文和数字before:{target}
- 和after相反between:{min},{max}
- 在min和max之间的数字confirmed:{target}
- 必须和target一样date_between:{min,max}
- 日期在min和max之间date_format:{format}
- 合法的format格式化日期decimal:{decimals?}
- 数字,而且是decimals进制digits:{length}
- 长度为length的数字dimensions:{width},{height}
- 符合宽高规定的图片email
- 不解释ext:[extensions]
- 后缀名image
- 图片in:[list]
- 包含在数组list内的值ip
- ipv4地址max:{length}
- 最大长度为length的字符mimes:[list]
- 文件类型min
- max相反mot_in
- in相反numeric
- 只允许数字regex:{pattern}
- 值必须符合正则patternrequired
- 不解释size:{kb}
- 文件大小不超过url:{domain?}
- (指定域名的)url
五、自定义校验规则
1.直接定义
1 2 3 4 | const validator = (value, args) => { // Return a Boolean or a Promise. } //最基本的形式,只返回布尔值或者Promise,带默认的错误提示 |
2.对象形式
1 2 3 4 5 6 7 8 | const validator = { getMessage(field, args) { // 添加到默认的英文错误消息里面 // Returns a message. }, validate(value, args) { // Returns a Boolean or a Promise. } }; |
3.添加到指定语言的错误消息
1 2 3 4 5 6 7 8 9 10 11 12 13 | const validator = { messages: { en: (field, args) => { // 英文错误提示 }, cn: (field, args) => { // 中文错误提示 } }, validate(value, args) { // Returns a Boolean or a Promise. } }; |
创建了规则之后,用extend方法添加到Validator里面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import { Validator } from 'vee-validate' ; const isMobile = { messages: { en:(field, args) => field + '必须是11位手机号码' , }, validate: (value, args) => { return value.length == 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/.test(value) } } Validator.extend( 'mobile' , isMobile); //或者直接 Validator.extend( 'mobile' , { messages: { en:field => field + '必须是11位手机号码' , }, validate: value => { return value.length == 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/.test(value) } }); 然后接可以直接把mobile当成内置规则使用了: <input v-validate data-rules= "required|mobile" : class = "{'input': true, 'is-danger': errors.has('mobile') }" name= "mobile" type= "text" placeholder= "Mobile" > <span v-show= "errors.has('mobile')" class = "help is-danger" >{{ errors.first( 'mobile' ) }}</span> |
4.自定义内置规则的错误信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import { Validator } from 'vee-validate' ; const dictionary = { en: { messages: { alpha: () => 'Some English Message' } }, cn: { messages: { alpha: () => '对alpha规则的错误定义中文描述' } } }; Validator.updateDictionary(dictionary); |
***********************************************************************************************
结合vant 使用
import VeeValidate, { Validator } from 'vee-validate';
import zh_CN from 'vee-validate/dist/locale/zh_CN';//引入中文文件
// 配置中文
Validator.addLocale(zh_CN);
const config = {
locale: 'zh_CN'
};
Vue.use(VeeValidate, config);
<van-field
v-model="xmpy"
placeholder=""
label="姓名拼音"
required
v-validate="'required'"
name="xmpy"
:class="{'van-field--error': errors.has('xmpy')}"
:error-message="errors.first('xmpy')"
/>