mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
docs(Form): add async validation document
This commit is contained in:
parent
8e9e0af17d
commit
57d59e8a86
@ -49,20 +49,15 @@ export default {
|
|||||||
console.log('submit', values);
|
console.log('submit', values);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
### Validate Rules
|
### Validate Rules
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<van-form @submit="onSubmit" @failed="onFailed">
|
<van-form @submit="onSubmit" @failed="onFailed">
|
||||||
<van-field
|
<van-field v-model="value" name="phone" label="Phone" :rules="rules.phone" />
|
||||||
v-model="value"
|
<van-field v-model="code" name="code" label="Code" :rules="rules.code" />
|
||||||
name="phone"
|
|
||||||
label="Phone"
|
|
||||||
:rules="rules"
|
|
||||||
placeholder="Phone"
|
|
||||||
/>
|
|
||||||
<div style="margin: 16px;">
|
<div style="margin: 16px;">
|
||||||
<van-button round block type="info">Submit</van-button>
|
<van-button round block type="info">Submit</van-button>
|
||||||
</div>
|
</div>
|
||||||
@ -70,15 +65,36 @@ export default {
|
|||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
import { Toast } from 'vant';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
this.rules = [
|
this.rules = {
|
||||||
{ required: true, message: 'Phone is required' },
|
phone: [
|
||||||
{ validator: val => /1\d{10}/.test(val), message: 'Incorrect format' },
|
{ required: true, message: 'Phone is required' },
|
||||||
];
|
{ validator: val => this.validatePhone, message: 'Incorrect phone' },
|
||||||
|
],
|
||||||
|
code: [
|
||||||
|
{ required: true, message: 'Code is required' },
|
||||||
|
{ validator: this.validateCode, message: 'Incorrect code' },
|
||||||
|
],
|
||||||
|
};
|
||||||
return { value: '' };
|
return { value: '' };
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
validatePhone(val) {
|
||||||
|
return /1\d{10}/.test(val);
|
||||||
|
},
|
||||||
|
validateCode(val) {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
Toast.loading('Validating...');
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
Toast.clear();
|
||||||
|
resolve(/\d{6}/.test(val));
|
||||||
|
}, 1000);
|
||||||
|
});
|
||||||
|
},
|
||||||
onSubmit(values) {
|
onSubmit(values) {
|
||||||
console.log('submit', values);
|
console.log('submit', values);
|
||||||
},
|
},
|
||||||
@ -334,7 +350,7 @@ export default {
|
|||||||
return {
|
return {
|
||||||
value: '',
|
value: '',
|
||||||
showArea: false,
|
showArea: false,
|
||||||
areaList: {}
|
areaList: {},
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
@ -383,7 +399,8 @@ export default {
|
|||||||
### Props
|
### Props
|
||||||
|
|
||||||
| Attribute | Description | Type | Default |
|
| Attribute | Description | Type | Default |
|
||||||
|------|------|------|------|
|
| --------- | ----------- | ---- | ------- |
|
||||||
|
|
||||||
|
|
||||||
### Events
|
### Events
|
||||||
|
|
||||||
@ -392,4 +409,5 @@ export default {
|
|||||||
### Slots
|
### Slots
|
||||||
|
|
||||||
| Name | Description |
|
| Name | Description |
|
||||||
|------|------|
|
| ---- | ----------- |
|
||||||
|
|
||||||
|
@ -60,17 +60,12 @@ export default {
|
|||||||
|
|
||||||
### 校验规则
|
### 校验规则
|
||||||
|
|
||||||
在`rules`中,可以通过`validator`字段定义校验函数,校验函数返回`false`时表示校验不通过,返回`Promise`时表示异步校验
|
在`rules`中,可以通过`validator`字段定义校验函数
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<van-form @submit="onSubmit" @failed="onFailed">
|
<van-form @submit="onSubmit" @failed="onFailed">
|
||||||
<van-field
|
<van-field v-model="value" name="phone" label="手机号" :rules="rules.phone" />
|
||||||
v-model="value"
|
<van-field v-model="code" name="code" label="验证码" :rules="rules.code" />
|
||||||
name="phone"
|
|
||||||
label="手机号"
|
|
||||||
:rules="rules"
|
|
||||||
placeholder="手机号"
|
|
||||||
/>
|
|
||||||
<div style="margin: 16px;">
|
<div style="margin: 16px;">
|
||||||
<van-button round block type="info">提交</van-button>
|
<van-button round block type="info">提交</van-button>
|
||||||
</div>
|
</div>
|
||||||
@ -78,15 +73,38 @@ export default {
|
|||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
import { Toast } from 'vant';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
this.rules = [
|
this.rules = {
|
||||||
{ required: true, message: '请输入手机号' },
|
phone: [
|
||||||
{ validator: val => /1\d{10}/.test(val), message: '手机号格式错误' },
|
{ required: true, message: '请输入手机号' },
|
||||||
];
|
{ validator: val => this.validatePhone, message: '手机号格式错误' },
|
||||||
|
],
|
||||||
|
code: [
|
||||||
|
{ required: true, message: '请输入验证码' },
|
||||||
|
{ validator: this.validateCode, message: '验证码错误' },
|
||||||
|
],
|
||||||
|
};
|
||||||
return { value: '' };
|
return { value: '' };
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
// 校验函数返回 true 表示校验通过,false 表示不通过
|
||||||
|
validatePhone(val) {
|
||||||
|
return /1\d{10}/.test(val);
|
||||||
|
},
|
||||||
|
// 校验函数返回 Promise 来实现异步校验
|
||||||
|
validateCode(val) {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
Toast.loading('验证中...');
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
Toast.clear();
|
||||||
|
resolve(/\d{6}/.test(val));
|
||||||
|
}, 1000);
|
||||||
|
});
|
||||||
|
},
|
||||||
onSubmit(values) {
|
onSubmit(values) {
|
||||||
console.log('submit', values);
|
console.log('submit', values);
|
||||||
},
|
},
|
||||||
|
@ -61,7 +61,7 @@ export default {
|
|||||||
phone: [
|
phone: [
|
||||||
{ required: true, message: this.$t('requirePhone') },
|
{ required: true, message: this.$t('requirePhone') },
|
||||||
{
|
{
|
||||||
validator: val => /1\d{10}/.test(val),
|
validator: this.validatePhone,
|
||||||
message: this.$t('incorrectPhone'),
|
message: this.$t('incorrectPhone'),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
@ -76,6 +76,10 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
|
validatePhone(val) {
|
||||||
|
return /1\d{10}/.test(val);
|
||||||
|
},
|
||||||
|
|
||||||
validateCode(val) {
|
validateCode(val) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
this.$toast.loading(this.$t('validating'));
|
this.$toast.loading(this.$t('validating'));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user