feat(Form): support async validation

This commit is contained in:
陈嘉涵
2020-02-12 10:38:05 +08:00
parent 35260e024a
commit 080d1828c8
5 changed files with 120 additions and 46 deletions

View File

@@ -1,13 +1,19 @@
<template>
<demo-block :title="$t('title')">
<van-form>
<van-form validate-first @sumbit="onSubmit" @failed="onFailed">
<van-field
v-model="value"
v-model="phone"
name="phone"
:label="$t('phone')"
:rules="rules"
:rules="rules.phone"
:placeholder="$t('phone')"
@submit="onSubmit"
@failed="onFailed"
/>
<van-field
v-model="code"
name="code"
:label="$t('code')"
:rules="rules.code"
:placeholder="$t('code')"
/>
<div style="margin: 16px 16px 0;">
<van-button type="info" round block>{{ $t('submit') }}</van-button>
@@ -20,35 +26,57 @@
export default {
i18n: {
'zh-CN': {
code: '验证码',
phone: '手机号',
title: '校验规则',
submit: '提交',
required: '请输入手机号',
incorrectFormat: '手机号格式错误',
validating: '验证中...',
incorrectCode: '验证码错误',
incorrectPhone: '手机号格式错误',
},
'en-US': {
code: 'Code',
phone: 'Phone',
title: 'Validate Rules',
submit: 'Submit',
required: 'Phone is required',
incorrectFormat: 'Incorrect format',
validating: 'Validating...',
incorrectCode: 'Incorrect code',
incorrectPhone: 'Incorrect phone',
},
},
data() {
return {
value: '',
code: '',
phone: '',
};
},
created() {
this.rules = [
{ required: true, message: this.$t('required') },
{
validator: val => /1\d{10}/.test(val),
message: this.$t('incorrectFormat'),
},
];
this.rules = {
phone: [
{ required: true, message: this.$t('required') },
{
validator: val => /1\d{10}/.test(val),
message: this.$t('incorrectPhone'),
},
],
code: [
{
validator: val =>
new Promise(resolve => {
this.$toast.loading(this.$t('validating'));
setTimeout(() => {
this.$toast.clear();
resolve(/\d{6}/.test(val));
}, 1000);
}),
message: this.$t('incorrectCode'),
},
],
};
},
methods: {