当前位置:Gxlcms > JavaScript > vue+VeeValidate 校验范围实例详解(部分校验,全部校验)

vue+VeeValidate 校验范围实例详解(部分校验,全部校验)

时间:2021-07-01 10:21:17 帮助过:26人阅读

搜索很久,没有发现有关于vue+VeeValidate部分校验的。自己写一个。

主要是两个场景:

1. 校验范围内,所有的字段。

2. 校验全局所有字段。

主要方法:

1.validate(fields, scope)     

2. validateAll(fields)

场景: 遍历得到多个列表,每一个列表都可以独立保存当前列表。在保存当前列表的时候,需要校验当前列表输入框的合法性。

代码:

  1. <div class=" col-xs-12 col-md-6 col-lg-4" v-for="(p1,index) in carList" :key="index">
  2. <div class="box box-success" style="margin-top: 15px;overflow: hidden;" >
  3. <div class="col-xs-7" style="border-right:1px solid #eee;padding-top: 10px;">
  4. <label class="col-xs-12 " style="padding: 5px 0;">车牌号: <span style="font-weight: normal;word-break:break-all;">{{p1.planLicenseNo}}</span></label>
  5. <label class="col-xs-12" style="padding: 5px 0;;">司机:<span style="font-weight: normal;word-break:break-all;">{{p1.planDriver}}</span></label>
  6. </div>
  7. <div class="col-xs-5" style="padding-top: 10px;">
  8. <div class="form-group" :class="{'has-error': errors.has('licenseNo' + index, 'newsletter' + index)}">
  9. <label >实际车牌号 <i class="errMsg">*</i></label>
  10. <input type="text" class="form-control" v-model.trim="p1.licenseNo"
  11. v-validate="{required: true}" :data-vv-scope="'newsletter' + index"
  12. :name="'licenseNo' + index" :data-vv-as="$t('pagefield.purchase.carCode')">
  13. <span v-show="errors.has('licenseNo' + index, 'newsletter' + index)" class="help-block">{{ errors.first('licenseNo' + index, 'newsletter' + index) }}</span>
  14. </div>
  15. <div class="form-group" :class="{'has-error': errors.has('actualQty' + index, 'newsletter' + index)}">
  16. <label >实际数量(头)<i class="errMsg">*</i></label>
  17. <input type="text" class="form-control" v-model.trim="p1.actualQty" :data-vv-scope="'newsletter' + index"
  18. v-validate="{required: true, decimal:2, min_value: 0, max_value: p1.planQty}"
  19. :name="'actualQty' + index" :data-vv-as="$t('message.quantity')">
  20. <span v-show="errors.has('actualQty' + index, 'newsletter' + index)" class="help-block">{{ errors.first('actualQty' + index, 'newsletter' + index) }}</span>
  21. </div>
  22. <div class="form-group" :class="{'has-error': errors.has('actualWgh' + index, 'newsletter' + index)}">
  23. <label>总重(kg) <i class="errMsg">*</i></label>
  24. <input type="text" class="form-control" v-model.trim="p1.actualWgh" :data-vv-scope="'newsletter' + index"
  25. v-validate="{required: true, decimal:2, min_value: 0, max_value: p1.planWgh}"
  26. :name="'actualWgh' + index" :data-vv-as="$t('message.weight')">
  27. <span v-show="errors.has('actualWgh' + index, 'newsletter' + index)" class="help-block">{{ errors.first('actualWgh' + index, 'newsletter' + index) }}</span>
  28. </div>
  29. <div class="form-group">
  30. <label>过磅单</label>
  31. <input type="text" class="form-control" v-model.trim="p1.weightNo">
  32. </div>
  33. </div>
  34. <div class="col-xs-12 text-right" style="border-top: 1px solid #eee;padding: 10px 15px;">
  35. <button class="btn btn-warning" @click="doSave(p1, index)">保存</button>
  36. </div>
  37. </div>
  38. </div>

* carList: [{}, {}]

* data-vv-scope: [type='string']  属性的值的类型是 string,表示定义了一个区域,在校验的时候,通过属性值 让validator 可以找到当前应该校验的区域。

此时通过 验证器提供的方法validate(scopeName)就可以校验当前区域了。

  1. doSave (obj, i) {
  2. var _self = this
  3. var validateScope = 'newsletter' + i
  4. this.$validator.validate(validateScope + '.*').then((result) => {
  5. if (result) {
  6. // 提交数据
  7. _self.doSaveAfterCheck()
  8. }
  9. })
  10. }
  11. /*
  12. errors.has(field, scope) 返回一个true / false
  13. errors.has('actualWgh' + index, 'newsletter' + index)}" 表示验证区域是 data-vv-scope = 'newsletter' + index 验证的字段是属性 name ='actualWgh' + index
  14. first(field,scope) 返回与特定字段关联或由选择器指定的第一条错误消息,前提是作用域将查找该范围内的消息,
  15. */
  16. <div class="form-group" :class="{'has-error': errors.has('actualWgh' + index, 'newsletter' + index)}">
  17. <label>总重(kg) <i class="errMsg">*</i></label>
  18. <input type="text" class="form-control" v-model.trim="p1.actualWgh" :data-vv-scope="'newsletter' + index"
  19. v-validate="{required: true, decimal:2, min_value: 0, max_value: p1.planWgh}"
  20. :name="'actualWgh' + index" :data-vv-as="$t('message.weight')">
  21. <span v-show="errors.has('actualWgh' + index, 'newsletter' + index)" class="help-block">{{ errors.first('actualWgh' + index, 'newsletter' + index) }}</span>
  22. </div>

场景2 : 页面有多个校验。当保存的时候,需要全部校验。这个场景官方提供2种方法.

  1. this.$validator.validate().then((result) => {
  2. if (result) {
  3. // 提交数据。
  4.       //   result是一个boolean值。true 表示没有触发错误规则,false 表示页面有非法值,触发错误
  5. _self.doSaveAfterCheck()
  6. }
  7. })
  8. this.$validator.validateAll().then((result) => {
  9. if (result) {
  10. // 提交数据。
  11. _self.doSaveAfterCheck()
  12. }
  13. })

上述两种校验全部的方法不同点在于适用场景:

validate() 可以指定校验范围内,或者是全局的 字段。而validateAll() 只能校验全局。

官方示例:

  1. // validate all fields.
  2. //  校验全局范围所有字段
  3. validator.validate(); === validateAll() 两个方法完全一样。
  4. // validate a field that has a matching name with the provided selector.
  5. // 校验哪个字段? field 取name的值。
  6. validator.validate('field');
  7. // validate a field within a scope.
  8. // 校验 某个域内 的某个字段。
  9. validator.validate('scope.field');
  10. // validate all fields within this scope.
  11. // 校验 某个域内的所有字段。 上述例子就是用的这个。 *_*
  12. validator.validate('scope.*');
  13. // validate all fields without a scope.
  14. // 校验没有定义域内的 字段。适用场景: 校验场景分为两种: 定义域的,没有定义域。
  15. //  当页面所有需要校验的字段,都定义了域,则这个方法会导致没有可校验的值,直接返回true
  16. validator.validate('*');

总结

以上所述是小编给大家介绍的vue+VeeValidate 校验范围实例详解(部分校验,全部校验),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

人气教程排行