feat(Form): improve validate、validateField method

This commit is contained in:
陈嘉涵 2020-02-13 15:36:28 +08:00
parent b4d117f59d
commit 7b200d0053
3 changed files with 12 additions and 10 deletions

View File

@ -420,9 +420,8 @@ Use [ref](https://vuejs.org/v2/api/#ref) to get Form instance and call instance
| Name | Description | Attribute | Return value | | Name | Description | Attribute | Return value |
|------|------|------|------| |------|------|------|------|
| validate | Validate all fields | - | *Promise* | | validate | Validate form | *name?: string* | *Promise* |
| validateField | Validate a filed | *name: string* | *Promise* | | resetValidation | Reset validation | *name?: string* | - |
| resetValidation | Reset all validation | - | - |
### Slots ### Slots

View File

@ -452,9 +452,8 @@ export default {
| 方法名 | 说明 | 参数 | 返回值 | | 方法名 | 说明 | 参数 | 返回值 |
|------|------|------|------| |------|------|------|------|
| validate | 验证所有表单项,返回 Promise验证不通过时 reject | - | *Promise* | | validate | 验证表单,支持传入`name`来验证单个表单项 | *name?: string* | *Promise* |
| validateField | 验证单个表单项,返回 Promise验证不通过时 reject | *name: string* | *Promise* | | resetValidation | 重置表单项的验证提示,支持传入`name`来重置单个表单项 | *name?: string* | - |
| resetValidation | 重置所有表单验证提示 | - | - |
### Slots ### Slots

View File

@ -63,11 +63,13 @@ export default createComponent({
}, },
// @exposed-api // @exposed-api
validate() { validate(name) {
if (name) {
return this.validateField(name);
}
return this.validateFirst ? this.validateSeq() : this.validateAll(); return this.validateFirst ? this.validateSeq() : this.validateAll();
}, },
// @exposed-api
validateField(name) { validateField(name) {
const matched = this.fields.filter(item => item.name === name); const matched = this.fields.filter(item => item.name === name);
@ -87,9 +89,11 @@ export default createComponent({
}, },
// @exposed-api // @exposed-api
resetValidation() { resetValidation(name) {
this.fields.forEach(item => { this.fields.forEach(item => {
item.resetValidation(); if (!name || item.name === name) {
item.resetValidation();
}
}); });
}, },