feat(Form): add validateField method

This commit is contained in:
陈嘉涵 2020-02-13 15:10:50 +08:00
parent 4059643f2f
commit b4d117f59d
4 changed files with 24 additions and 3 deletions

View File

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

View File

@ -452,7 +452,8 @@ export default {
| 方法名 | 说明 | 参数 | 返回值 |
|------|------|------|------|
| validate | 触发表单验证 | - | *Promise\<void\>* |
| validate | 验证所有表单项,返回 Promise验证不通过时 reject | - | *Promise* |
| validateField | 验证单个表单项,返回 Promise验证不通过时 reject | *name: string* | *Promise* |
| resetValidation | 重置所有表单验证提示 | - | - |
### Slots

View File

@ -1,6 +1,6 @@
<template>
<demo-block :title="$t('basicUsage')">
<van-form @submit="onSubmit" @failed="onFailed">
<van-form ref="form" @submit="onSubmit" @failed="onFailed">
<van-field
v-model="username"
name="username"

View File

@ -67,6 +67,25 @@ export default createComponent({
return this.validateFirst ? this.validateSeq() : this.validateAll();
},
// @exposed-api
validateField(name) {
const matched = this.fields.filter(item => item.name === name);
if (matched.length) {
return new Promise((resolve, reject) => {
matched[0].validate().then(error => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
return Promise.reject();
},
// @exposed-api
resetValidation() {
this.fields.forEach(item => {