检验规则代码中,我们可以看到规则对象包含required/message/trigger/min/max参数,这些参数配合起来,可以完成我们以往要编写多行代码才能实现校验功能

下面就详细介绍

参数说明

属性

类型

说明

type

String

用于验证数据类型默认类型为’string

required

boolean

是否必填

pattern

regexp/string

字段匹配正则表达式才能通过验证

minmax

number

对于字符串数组类型,将根据长度进行比较,对于数字类型数字不得小于min,也不得大于max

len

number

对于字符串数组类型,对length属性执行比较,对于数字类型,此属性指示数字的完全匹配len属性与minmax属性组合,则len优先

enum

array

type必须设置enum,值必须包含在从可能列表中才能通过验证

trigger

blur/change

触发验证的时机,blur失去焦点时触发,change:值发生改变时触发

whitespace

boolean

type必须设置string,要为仅包含空格字符串可以whitespace设置true

transform

function

验证之前转换

defaultField

array/object

type数组对象类型时一起使用用来验证数组对象包含的所有值,进行深层验证

fields

object

type数组对象类型时一起使用,分别验证array/object类型的数据中的值,实现深度验证

validator

function

验证器,可以指定字段自定义验证函数function(rule, value, callback)

asyncValidator

function

异步验证器,可以为指定字段自定义异步验证函数 function(rule, value, callback)

message

string/jsx/function

根据需要消息分配给规则

下面详细介绍参数属性说明

type

表明要使用验证器的类型,类似数据格式检验,其中还有email、urlregexpmethod等特定格式字段的验证。

使用这个我们就可以对一些特定的字段进行校验,而不用再像以前一样写正则,做判断

比如需要配置 type:‘email’ 的规则就可以验证email了,验证器都已经封装好了这些功能,你只需调用就可以了。

可识别的类型值有:

//验证电子邮箱完整示例代码
email = [{
    type: "string",
    required: true,
    message: '请输入邮箱地址',
    trigger: 'blur'
  },
  {
    type: 'email',
    message: '请输入正确邮箱地址',
    trigger: ['blur', 'change']
  }
]

如上,实现文本框失去焦点进行非空检验,失去焦点、内容改变进行格式验证,并给出相应提示.

required

必填字段,即非空验证。如上面实例中的的非空验证,以及邮箱前边的必填符号*,就是这个参数的功劳。

pattern

正则表达式,如果需要验证手机号码之类,可以直接编写正则表达式配置校验规则中,那么就不需要自己校验了,由校验器自动校验。

{ type : "string" , required: true , pattern : /^[a-z]+$/ }

min/max

判断数据大小范围,通常对数字大小范围做校验。对于字符串和数组类型,将根据长度进行比较

{ required: true, message: '请输入活动名称', trigger: 'blur' },
{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }

len

长度验证,如11位手机号码。

roles: {
    type: "array", required: true, len: 11
}

trigger

取值blur/change, blur失去焦点时触发,change:值发生改变时触发

enum

枚举值验证,示例代码如下

role: {type: "enum", enum: ['admin', 'user', 'guest']}

whitespace

验证是否只有空格(如果没有配置,则全空格输入值也是有效的)。

whitespace: [{
    type: "string",
    message: '只存在空格',
    whitespace:true,
    trigger: ['change', 'blur']
}]

transform

有时有必要在验证之前转换值,以强制或以某种方式对其进行清理。为此 transform ,向验证规则添加一个功能。在验证之前,先转换属性,然后将其重新分配给源对象,以更改该属性的值。

transform: [
 {
    type: 'enum',
    enum: [2,4,6], 
    message: `结果存在`, 
    trigger: ['change', 'blur'],
    transform(value) {
      return Number(value * 2)
    }
  }
]

如上,只有输入1、2、3的时候才能校验通过,这个只能辅助校验,并不能改变组件绑定变量本身的值。

fields

深层规则,可以通过嵌套规则分配给规则的属性来验证objectarray类型的验证规则,如地址对象省市区的规则验证:

object类型:
address: {
    type: "object", required: true,
    fields: {
      street: {type: "string", required: true},
      city: {type: "string", required: true},
      zip: {type: "string", required: true, len: 8, message: "invalid zip"}
    }
  }
array类型:
 roles: {
    type: "array", required: true, len: 3,
    fields: {
      0: {type: "string", required: true},
      1: {type: "string", required: true},
      2: {type: "string", required: true}
    }
  }

messages

通过校验的提示信息

{name:{type: "string", required: true, message: "Name is required"}}

支持html:

{name:{type: "string", required: true, message: "<b>Name is required</b>"}}

支持vue-i18n:

{name:{type: "string", required: true, message: () => this.$t( 'name is required' )}}

validator

可以为指定字段自定义验证函数——这就相当于把前边配置的东西用js按照以前的方式编写验证逻辑了。虽然麻烦点,但是能实现比较复杂业务逻辑判断

例如判断密码是否为空

data(){
       var checkPass= (rules,value,callback) =>{
            if(value ==''){
                callback(new Error('请输入密码'));
            }else{
                callback();
            }
        }
        return{
            rules:{
                pass:[vaildatot:checkPass,trigger:blur]
            }
        }
}

原文地址:https://blog.csdn.net/changwen17/article/details/129057065

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_37786.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注