Ant Design Vue Form表单校验相关问题

本文讲述了在开发中如何实现对借款合同编号的特定校验,以及如何利用Vue表单验证库的validate、validateFields和clearValidate方法来控制不同场景下的验证策略,同时提醒开发者优先查阅官方文档解决问题。
摘要由CSDN通过智能技术生成

1.先讲实现需求

1.新增、编辑页面点击“保存”时,只针对“借款合同编号”进行校验。
2.提交、批量提交时需要进行校验。
3.表单“保证金”字段不进行校验。  



2.实现方案

rules表单验证规则
validate触发表单验证,  注意:会对整个表单进行校验,只要绑定了 name属性的表单项 ,都会被校验。
validateFields触发表单验证,可以只针对部分表单校验(nameList?: NamePath[]) => Promise
clearValidate移除表单项的校验结果。传入待移除的表单项的 name 属性或者 name 组成的数组,如不传则移除整个表单的校验结果(nameList?: NamePath[]) => void
required是否必填,如不设置,则会根据校验规则自动生成boolean


1.使用 rules: [{ required: false, type: 'loanContractCode', message: '请输入正确内容' }] 根据required: false 设置是否进行校验。
2.使用 validateFields 可以实现针对部分表单校验,例:validateFields( ['loanContractCode'] )
3.使用 clearValidate可以移出校验结果,避免影响用户体验。
效果展示:


2.代码实现

 

注意:required 要写到 rules[ {} ] 里面,这边是因为我们对校验方法进行了二次封装才写到外面的。例如:rules: [ { required: false, pattern: regMap, message: '请输入' } ]。
 

4.结语

小建议:遇到问题先看官方文档,去查看官方api是解决方法的第一要素。

每天一句:🐼生命如同寓言,其价值不在于长短,而在于内容。

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,ant-design-vue可以通过内置的Form组件和Table组件实现格内部字段验证功能。具体步骤如下: 1. 在格中添加需要验证的字段,例如下面的代码中的name和age字段: ```html <a-form :form="form"> <a-form-item label="Name" :rules="[{ required: true, message: 'Please input name' }]"> <a-input v-decorator="['name']" /> </a-form-item> <a-form-item label="Age" :rules="[{ required: true, message: 'Please input age' }]"> <a-input-number v-decorator="['age']" /> </a-form-item> </a-form> <a-table :columns="columns" :dataSource="dataSource" :pagination="false" /> ``` 2. 在格中添加操作列,例如下面的代码中的操作列包含了编辑和删除按钮: ```html <a-table :columns="columns" :dataSource="dataSource" :pagination="false"> <template #action="text, record"> <a-button @click="edit(record)">Edit</a-button> <a-button @click="delete(record)">Delete</a-button> </template> </a-table> ``` 3. 在编辑操作中打开,并将当前行的数据绑定到中: ```javascript edit(record) { this.form.setFieldsValue(record); this.editingKey = record.key; } ``` 4. 在中添加保存按钮,并在点击保存按钮时进行验证和数据更新: ```html <a-form :form="form"> <a-form-item label="Name" :rules="[{ required: true, message: 'Please input name' }]"> <a-input v-decorator="['name']" /> </a-form-item> <a-form-item label="Age" :rules="[{ required: true, message: 'Please input age' }]"> <a-input-number v-decorator="['age']" /> </a-form-item> <a-form-item> <a-button type="primary" @click="save">Save</a-button> </a-form-item> </a-form> ``` ```javascript save() { this.form.validateFields((err, values) => { if (!err) { const newData = [...this.dataSource]; const index = newData.findIndex((item) => this.editingKey === item.key); if (index > -1) { const item = newData[index]; newData.splice(index, 1, { ...item, ...values }); this.dataSource = newData; this.editingKey = ''; } else { newData.push(values); this.dataSource = newData; this.editingKey = ''; } } }); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值