vue+VeeValidate 校驗範圍(部分校驗,所有校驗)

搜索好久,沒有發現有關於vue+VeeValidate部分校驗的。本身寫一個。vue

主要是兩個場景: 1. 校驗範圍內,全部的字段。 2. 校驗全局全部字段。主要方法: 1.validate(fields, scope)      2. validateAll(fields)ide

場景: 遍歷獲得多個列表,每個列表均可以獨立保存當前列表。在保存當前列表的時候,須要校驗當前列表輸入框的合法性。ui

代碼:this

   <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;;">司機:<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 >實際車牌號 <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 >實際數量(頭)<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>
View Code

* carList: [{}, {}]spa

* data-vv-scope: [type='string']  屬性的值的類型是 string,表示定義了一個區域,在校驗的時候,經過屬性值 讓validator 能夠找到當前應該校驗的區域。code

此時經過 驗證器提供的方法validate(scopeName)就能夠校驗當前區域了。orm

doSave (obj, i) {
        var _self = this
        var validateScope = 'newsletter' + i
        this.$validator.validate(validateScope + '.*').then((result) => {
          if (result) {
            //  提交數據
            _self.doSaveAfterCheck()
          }
        })
      }
/*
errors.has(field, scope) 返回一個true / false
errors.has('actualWgh' + index, 'newsletter' + index)}"  表示驗證區域是 data-vv-scope = 'newsletter' + index  驗證的字段是屬性 name ='actualWgh' + index
first(field,scope) 返回與特定字段關聯或由選擇器指定的第一條錯誤消息,前提是做用域將查找該範圍內的消息,
*/
<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 : 頁面有多個校驗。當保存的時候,須要所有校驗。這個場景官方提供2種方法.

this.$validator.validate().then((result) => {
        if (result) {
          //  提交數據。
      //   result是一個boolean值。true 表示沒有觸發錯誤規則,false 表示頁面有非法值,觸發錯誤
_self.doSaveAfterCheck() } })

 

this.$validator.validateAll().then((result) => {
        if (result) {
          //  提交數據。
          _self.doSaveAfterCheck()
        }
      })

 

上述兩種校驗所有的方法不一樣點在於適用場景:

validate() 能夠指定校驗範圍內,或者是全局的 字段。而validateAll() 只能校驗全局。blog

官方示例: ci

// validate all fields.
//  校驗全局範圍全部字段
validator.validate(); === validateAll() 兩個方法徹底同樣。

// validate a field that has a matching name with the provided selector.
//  校驗哪一個字段? field 取name的值。
validator.validate('field');

// validate a field within a scope.
// 校驗 某個域內 的某個字段。
validator.validate('scope.field');

// validate all fields within this scope.
// 校驗 某個域內的全部字段。   上述例子就是用的這個。   *_* 
validator.validate('scope.*');

// validate all fields without a scope.
// 校驗沒有定義域內的 字段。適用場景: 校驗場景分爲兩種: 定義域的,沒有定義域。//  當頁面全部須要校驗的字段,都定義了域,則這個方法會致使沒有可校驗的值,直接返回true
validator.validate('*');
相關文章
相關標籤/搜索