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')"
/>
相關文章
相關標籤/搜索