官网:https://baianat.github.io/vee-validate/vue
1
|
npm install vee-validate --save
|
直接安装会报错:
node
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,待研究git
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.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
被组件自动注入到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
|
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
2
3
4
|
const validator = (value, args) => {
// Return a Boolean or a Promise.
}
//最基本的形式,只返回布尔值或者Promise,带默认的错误提示
|
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.
}
};
|
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>
|
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 使用