mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(Form): add validateField method
This commit is contained in:
parent
4059643f2f
commit
b4d117f59d
@ -420,7 +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 | Trigger validation | - | *Promise\<void\>* |
|
| validate | Validate all fields | - | *Promise* |
|
||||||
|
| validateField | Validate a filed | *name: string* | *Promise* |
|
||||||
| resetValidation | Reset all validation | - | - |
|
| resetValidation | Reset all validation | - | - |
|
||||||
|
|
||||||
### Slots
|
### Slots
|
||||||
|
@ -452,7 +452,8 @@ export default {
|
|||||||
|
|
||||||
| 方法名 | 说明 | 参数 | 返回值 |
|
| 方法名 | 说明 | 参数 | 返回值 |
|
||||||
|------|------|------|------|
|
|------|------|------|------|
|
||||||
| validate | 触发表单验证 | - | *Promise\<void\>* |
|
| validate | 验证所有表单项,返回 Promise,验证不通过时 reject | - | *Promise* |
|
||||||
|
| validateField | 验证单个表单项,返回 Promise,验证不通过时 reject | *name: string* | *Promise* |
|
||||||
| resetValidation | 重置所有表单验证提示 | - | - |
|
| resetValidation | 重置所有表单验证提示 | - | - |
|
||||||
|
|
||||||
### Slots
|
### Slots
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<demo-block :title="$t('basicUsage')">
|
<demo-block :title="$t('basicUsage')">
|
||||||
<van-form @submit="onSubmit" @failed="onFailed">
|
<van-form ref="form" @submit="onSubmit" @failed="onFailed">
|
||||||
<van-field
|
<van-field
|
||||||
v-model="username"
|
v-model="username"
|
||||||
name="username"
|
name="username"
|
||||||
|
@ -67,6 +67,25 @@ export default createComponent({
|
|||||||
return this.validateFirst ? this.validateSeq() : this.validateAll();
|
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
|
// @exposed-api
|
||||||
resetValidation() {
|
resetValidation() {
|
||||||
this.fields.forEach(item => {
|
this.fields.forEach(item => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user