vue+VeeValidate 校驗(yàn)范圍實(shí)例詳解(部分校驗(yàn),全部校驗(yàn))
搜索很久,沒有發(fā)現(xiàn)有關(guān)于vue+VeeValidate部分校驗(yàn)的。自己寫一個。
主要是兩個場景:
1. 校驗(yàn)范圍內(nèi),所有的字段。
2. 校驗(yàn)全局所有字段。
主要方法:
1.validate(fields, scope)
2. validateAll(fields)
場景: 遍歷得到多個列表,每一個列表都可以獨(dú)立保存當(dāng)前列表。在保存當(dāng)前列表的時候,需要校驗(yàn)當(dāng)前列表輸入框的合法性。
代碼:
<div class=" col-xs-12 col-md-6 col-lg-4" v-for="(p1,index) in carList" :key="index">
<div class="box box-success" style="margin-top: 15px;overflow: hidden;" >
<div class="col-xs-7" style="border-right:1px solid #eee;padding-top: 10px;">
<label class="col-xs-12 " style="padding: 5px 0;">車牌號: <span style="font-weight: normal;word-break:break-all;">{{p1.planLicenseNo}}</span></label>
<label class="col-xs-12" style="padding: 5px 0;;">司機(jī):<span style="font-weight: normal;word-break:break-all;">{{p1.planDriver}}</span></label>
</div>
<div class="col-xs-5" style="padding-top: 10px;">
<div class="form-group" :class="{'has-error': errors.has('licenseNo' + index, 'newsletter' + index)}">
<label >實(shí)際車牌號 <i class="errMsg">*</i></label>
<input type="text" class="form-control" v-model.trim="p1.licenseNo"
v-validate="{required: true}" :data-vv-scope="'newsletter' + index"
:name="'licenseNo' + index" :data-vv-as="$t('pagefield.purchase.carCode')">
<span v-show="errors.has('licenseNo' + index, 'newsletter' + index)" class="help-block">{{ errors.first('licenseNo' + index, 'newsletter' + index) }}</span>
</div>
<div class="form-group" :class="{'has-error': errors.has('actualQty' + index, 'newsletter' + index)}">
<label >實(shí)際數(shù)量(頭)<i class="errMsg">*</i></label>
<input type="text" class="form-control" v-model.trim="p1.actualQty" :data-vv-scope="'newsletter' + index"
v-validate="{required: true, decimal:2, min_value: 0, max_value: p1.planQty}"
:name="'actualQty' + index" :data-vv-as="$t('message.quantity')">
<span v-show="errors.has('actualQty' + index, 'newsletter' + index)" class="help-block">{{ errors.first('actualQty' + index, 'newsletter' + index) }}</span>
</div>
<div class="form-group" :class="{'has-error': errors.has('actualWgh' + index, 'newsletter' + index)}">
<label>總重(kg) <i class="errMsg">*</i></label>
<input type="text" class="form-control" v-model.trim="p1.actualWgh" :data-vv-scope="'newsletter' + index"
v-validate="{required: true, decimal:2, min_value: 0, max_value: p1.planWgh}"
:name="'actualWgh' + index" :data-vv-as="$t('message.weight')">
<span v-show="errors.has('actualWgh' + index, 'newsletter' + index)" class="help-block">{{ errors.first('actualWgh' + index, 'newsletter' + index) }}</span>
</div>
<div class="form-group">
<label>過磅單</label>
<input type="text" class="form-control" v-model.trim="p1.weightNo">
</div>
</div>
<div class="col-xs-12 text-right" style="border-top: 1px solid #eee;padding: 10px 15px;">
<button class="btn btn-warning" @click="doSave(p1, index)">保存</button>
</div>
</div>
</div>
* carList: [{}, {}]
* data-vv-scope: [type='string'] 屬性的值的類型是 string,表示定義了一個區(qū)域,在校驗(yàn)的時候,通過屬性值 讓validator 可以找到當(dāng)前應(yīng)該校驗(yàn)的區(qū)域。
此時通過 驗(yàn)證器提供的方法validate(scopeName)就可以校驗(yàn)當(dāng)前區(qū)域了。
doSave (obj, i) {
var _self = this
var validateScope = 'newsletter' + i
this.$validator.validate(validateScope + '.*').then((result) => {
if (result) {
// 提交數(shù)據(jù)
_self.doSaveAfterCheck()
}
})
}
/*
errors.has(field, scope) 返回一個true / false
errors.has('actualWgh' + index, 'newsletter' + index)}" 表示驗(yàn)證區(qū)域是 data-vv-scope = 'newsletter' + index 驗(yàn)證的字段是屬性 name ='actualWgh' + index
first(field,scope) 返回與特定字段關(guān)聯(lián)或由選擇器指定的第一條錯誤消息,前提是作用域?qū)⒉檎以摲秶鷥?nèi)的消息,
*/
<div class="form-group" :class="{'has-error': errors.has('actualWgh' + index, 'newsletter' + index)}">
<label>總重(kg) <i class="errMsg">*</i></label>
<input type="text" class="form-control" v-model.trim="p1.actualWgh" :data-vv-scope="'newsletter' + index"
v-validate="{required: true, decimal:2, min_value: 0, max_value: p1.planWgh}"
:name="'actualWgh' + index" :data-vv-as="$t('message.weight')">
<span v-show="errors.has('actualWgh' + index, 'newsletter' + index)" class="help-block">{{ errors.first('actualWgh' + index, 'newsletter' + index) }}</span>
</div>
場景2 : 頁面有多個校驗(yàn)。當(dāng)保存的時候,需要全部校驗(yàn)。這個場景官方提供2種方法.
this.$validator.validate().then((result) => {
if (result) {
// 提交數(shù)據(jù)。
// result是一個boolean值。true 表示沒有觸發(fā)錯誤規(guī)則,false 表示頁面有非法值,觸發(fā)錯誤
_self.doSaveAfterCheck()
}
})
this.$validator.validateAll().then((result) => {
if (result) {
// 提交數(shù)據(jù)。
_self.doSaveAfterCheck()
}
})
上述兩種校驗(yàn)全部的方法不同點(diǎn)在于適用場景:

validate() 可以指定校驗(yàn)范圍內(nèi),或者是全局的 字段。而validateAll() 只能校驗(yàn)全局。
官方示例:
// validate all fields.
// 校驗(yàn)全局范圍所有字段
validator.validate(); === validateAll() 兩個方法完全一樣。
// validate a field that has a matching name with the provided selector.
// 校驗(yàn)?zāi)膫€字段? field 取name的值。
validator.validate('field');
// validate a field within a scope.
// 校驗(yàn) 某個域內(nèi) 的某個字段。
validator.validate('scope.field');
// validate all fields within this scope.
// 校驗(yàn) 某個域內(nèi)的所有字段。 上述例子就是用的這個。 *_*
validator.validate('scope.*');
// validate all fields without a scope.
// 校驗(yàn)沒有定義域內(nèi)的 字段。適用場景: 校驗(yàn)場景分為兩種: 定義域的,沒有定義域。
// 當(dāng)頁面所有需要校驗(yàn)的字段,都定義了域,則這個方法會導(dǎo)致沒有可校驗(yàn)的值,直接返回true
validator.validate('*');
總結(jié)
以上所述是小編給大家介紹的vue+VeeValidate 校驗(yàn)范圍實(shí)例詳解(部分校驗(yàn),全部校驗(yàn)),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue項(xiàng)目中如何將數(shù)據(jù)導(dǎo)出為word文檔
這篇文章主要介紹了vue項(xiàng)目中如何將數(shù)據(jù)導(dǎo)出為word文檔問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
詳解Vue template 如何支持多個根結(jié)點(diǎn)
這篇文章主要介紹了詳解Vue template 如何支持多個根結(jié)點(diǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
vuejs router history 配置到iis的方法
今天小編就為大家分享一篇vuejs router history 配置到iis的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue2.0 better-scroll 實(shí)現(xiàn)移動端滑動的示例代碼
本篇文章主要介紹了vue2.0 better-scroll 實(shí)現(xiàn)移動端滑動的示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2018-01-01
element-ui中select下拉框加載大數(shù)據(jù)渲染優(yōu)化方式
這篇文章主要介紹了element-ui中select下拉框加載大數(shù)據(jù)渲染優(yōu)化方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
使用vue3實(shí)現(xiàn)數(shù)據(jù)漏斗圖
漏斗圖一般會借助一些第三方的庫來實(shí)現(xiàn),這些庫使用起來雖然簡單順手,但是如果要定制樣式就會不太方便,本文我們就來用vue3手?jǐn)]一個漏斗圖吧2024-11-11
vue3中封裝axios請求最新實(shí)現(xiàn)步驟
這篇文章主要給大家介紹了關(guān)于vue3中封裝axios請求的最新實(shí)現(xiàn)步驟,在Vue 3中可以通過封裝axios來實(shí)現(xiàn)接口的統(tǒng)一管理和調(diào)用,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04

