惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - limeiky

无法打开 物理 文件 XXX.mdf"。操作系统错误 5:"5(拒绝访问。)"的解决办法 【2026】最简单的白嫖百度文库方法 如何做到子DIV相对DIV底部对齐 idea 连接 MySQL 8.0 以上遇到 Access denied for user ‘root‘@‘localhost‘ (using password: YES)密码错误的问题 idea解决程序包不存在报错 正则表达式解析 Vue之slot插槽和作用域插槽 SQL更新固定时间显示格式的时间字段 Vue中 let _this = this的作用 vue中的箭头函数 => vue-router 基本使用 如何限制同一用户同时在不同客户端登录? c:forEach 标签中遍历map集合 qrcode.js插件,将文字内容转换成二维码格式 JS生成一维码(条形码)功能示例 qrcode.js插件,将文字内容转换成二维码格式 在一个DaoImpl实现中调用另一个DaoImpl中的方法 echarts柱状图坐标文字显示不完整解决方式 Myeclipse在debug模式下没加断点程序卡住,start模式下可以正常启动
ant design vue 中的表单校验 v-decorator的使用
limeiky · 2022-02-16 · via 博客园 - limeiky

转自:《 ant design vue 中的表单校验 v-decorator》

1.v-decorator和v-model区别
v-decorator在我看来更加适合于有验证的操作,

比如不为空,唯一值,只能是数字等等。而v-model更加适合用来定义一些组件返回的接收参数,更好处理和获取,以及一些默认隐藏或者不显示但保存或修改需要用到的参数。

2.1 获取v-decorator的值

methods: {
    changeMe(selectedValue) {
        //selectedValue就是当前触发字段的值,也可以用下getFieldValue获取,但是有些组件好像获取在赋值前,可能需要加延迟
        this.form.getFieldValue('name')
     } 
}

2.2 设置v-decorator的值

methods: {
    changeMe(selectedValue) {
        this.form.setFieldsValue({
             name: "值",//
             id: res.result.id,//最后一个逗号随意
           });
     } 
}

2.3 v-decorator在form表单的验证:

部分前端代码

......
<a-form-item label="名字" :labelCol="XTwoLabelCol" :wrapperCol="XTwoWrapperCol" class="x-form-item">
 <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入"></a-input>
</a-form-item>
<a-form-item label="数字" :labelCol="XTwoLabelCol" :wrapperCol="XTwoWrapperCol" class="x-form-item">
 <a-input v-decorator="['number', validatorRules.number]" placeholder="请输入"></a-input>
</a-form-item>
......

验证:

data() {
   return {
           form: this.$form.createForm(this),
           validatorRules: {
          name: {//name与v-decorator中属性对应
            initialValue: "水",//初始化值,也就是默认值 
            rules: [
              { required: true, message: '请输入检验日期!'},//此处开启校验必填
              {validator: (rule, value, callback) => validateDuplicateValue('em_project_info', 'project_name', value, this.model.id, callback)},//此处开启唯一验证,
              { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},//此处配置正则表达式,手机号,可自己配置正则表达式
            ]
            },
          number: {//name与v-decorator中属性对应
            rules: [
              { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},//此处配置正则表达式,手机号,可自己配置正则表达式
            ]
            },
        },
        
   }
}

校验配置:

默认时间:

initialValue: moment(new Date()).format("YYYY-MM-DD"),//,此处一定要导入import moment from "moment"; 

必填:

rules: [
    { required: true, message: '请输入检验日期!'}
]

校验唯一值:

rules: [
     {validator: (rule, value, callback) => validateDuplicateValue('表名', '验证唯一值的字段', value, this.model.id, callback)},
]

正则表达式验证:

rules: [
     { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},//pattern为正则表达式,message为提示信息,正则表达式可以根据自己需求来设置。此处注意在/正则表达式/。在俩斜杠之间位正则表达式。
]

2.4 JEditableTable的验证:

部分代码:此处包含唯一验证,必填验证,正则表达式验证

columns: [
 {
   title:'名称',
   align:"center",//居中显示
   width: '280px',//宽度
   ellipsis: true,
   key: 'name',//字段名
   type: FormTypes.input,//表示以input标签显示
   placeholder: '请输入${title}',//提示信息
   validateRules: [
     {
       required: true,//在前端设置此字段必填
       message: '${title}不能为空',//在前端设置此字段不能为null,提示文本
       // 自定义函数校验 handler,表单验证,此处为唯一性验证
       handler(type, value, row, column, callback, target) {
         //验证sampleName不能重复
         let { values } = target.getValuesSync({ validate: false })
         let count = 0
         for (let val of values) {
           if (val['name'] === value) {//什么字段就传什么参数,
             if (++count >= 2) {
               callback(false, '${title}不能重复')
               return
             }
           }
         }
         callback(true) // true = 通过验证
       }
     }
   ],
 },
 {
   title:'数量',
   align:"center",
   //dataIndex: 'number'
   width: '140px',
   ellipsis: true,
   key: 'number',
   type: FormTypes.input,
   placeholder: '请输入${title}',
   validateRules: [
     { pattern:  /^[1-9]\d*$/, message: '请输入正整数!'},//正则表达式验证,正整数
   ]
 },
}

2.5 v-decorator的其他知识点

去空字符串

v-decorator.trim="[ 'dictCode', validatorRules.dictCode]"//v-decorator.trim为去空操作

rules:[{validator:(rule,value,callback)=>validateDuplicateValue('表名','验证唯一值的字段',value,this.model.id,callback)},]