forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseForm-trigger.vue
80 lines (73 loc) · 1.94 KB
/
useForm-trigger.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<docs>
---
order: 10
title:
zh-CN: useForm 自定义触发时机
en-US: useForm custom trigger
---
## zh-CN
通过 [`Form.useForm`](#useform) 自定义触发校验时机
## en-US
use [`Form.useForm`](#useform) custom trigger to validation logic and status.
</docs>
<template>
<a-form :label-col="labelCol" :wrapper-col="wrapperCol">
<a-form-item label="Activity name" v-bind="validateInfos.name">
<a-input
v-model:value="modelRef.name"
@blur="validate('name', { trigger: 'blur' }).catch(() => {})"
/>
</a-form-item>
<a-form-item label="Activity zone" v-bind="validateInfos.region">
<a-select v-model:value="modelRef.region" placeholder="please select your zone">
<a-select-option value="shanghai">Zone one</a-select-option>
<a-select-option value="beijing">Zone two</a-select-option>
</a-select>
</a-form-item>
<a-form-item :wrapper-col="{ span: 14, offset: 4 }">
<a-button type="primary" @click.prevent="onSubmit">Create</a-button>
<a-button style="margin-left: 10px" @click="resetFields">Reset</a-button>
</a-form-item>
</a-form>
</template>
<script lang="ts" setup>
import { reactive, toRaw } from 'vue';
import { Form } from 'ant-design-vue';
const useForm = Form.useForm;
const labelCol = { span: 4 };
const wrapperCol = { span: 14 };
const modelRef = reactive({
name: '',
region: undefined,
});
const rulesRef = reactive({
name: [
{
required: true,
message: 'Please input Activity name',
},
{
min: 3,
max: 5,
message: 'Length should be 3 to 5',
trigger: 'blur',
},
],
region: [
{
required: true,
message: 'Please select region',
},
],
});
const { resetFields, validate, validateInfos } = useForm(modelRef, rulesRef);
const onSubmit = () => {
validate()
.then(() => {
console.log(toRaw(modelRef));
})
.catch(err => {
console.log('error', err);
});
};
</script>