vee-validate 使用

官网:https://baianat.github.io/vee-validate/vue

1、安装

1
npm install vee-validate --save

直接安装会报错:node

__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

2、引用

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,待研究git

3、基础使用

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可能不会对其进行正确的校验github

关于errors:

上面的代码咱们看到有errors.has,errors.first,errors是组件内置的一个数据模型,用来存储和处理错误信息,能够调用如下方法:npm

  • 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以后设置对象参数dom

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

4、内置的校验规则

  • 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} - 值必须符合正则pattern
  • required - 不解释
  • size:{kb} - 文件大小不超过
  • url:{domain?} - (指定域名的)url

5、自定义校验规则

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里面ui

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);

***********************************************************************************************url

结合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')"
/>
相关文章
相关标签/搜索