CET-vue-3.0/src/views/demo/form/RuleForm.vue

261 lines
6.0 KiB
Vue
Raw Normal View History

2021-10-20 14:32:09 +08:00
<template>
2022-06-10 10:44:44 +08:00
<PageWrapper title="表单校验示例">
<div class="mb-4">
<a-button @click="validateForm" class="mr-2"> 手动校验表单</a-button>
<a-button @click="resetValidate" class="mr-2"> 清空校验信息</a-button>
<a-button @click="getFormValues" class="mr-2"> 获取表单值</a-button>
<a-button @click="setFormValues" class="mr-2"> 设置表单值</a-button>
<a-button @click="resetFields" class="mr-2"> 重置</a-button>
</div>
<CollapseContainer title="表单校验">
<BasicForm @register="register" @submit="handleSubmit" />
</CollapseContainer>
</PageWrapper>
2021-10-20 14:32:09 +08:00
</template>
<script lang="ts">
2022-06-10 10:44:44 +08:00
import { defineComponent } from 'vue';
import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
import { CollapseContainer } from '/@/components/Container';
import { useMessage } from '/@/hooks/web/useMessage';
import { PageWrapper } from '/@/components/Page';
import { isAccountExist } from '/@/api/demo/system';
2021-10-20 14:32:09 +08:00
2022-06-10 10:44:44 +08:00
const schemas: FormSchema[] = [
{
field: 'field1',
component: 'Input',
label: '字段1',
colProps: {
span: 8,
},
required: true,
},
{
field: 'field2',
component: 'Input',
label: '字段2',
colProps: {
span: 8,
},
required: true,
},
{
field: 'id',
label: 'id',
required: true,
defaultValue: 0,
component: 'InputNumber',
show: false,
},
{
field: 'field3',
component: 'DatePicker',
label: '字段3',
colProps: {
span: 8,
},
required: true,
},
{
field: 'field33',
component: 'DatePicker',
label: '字段33',
colProps: {
span: 8,
},
componentProps: {
valueFormat: 'YYYY-MM-DD',
},
rules: [{ required: true, type: 'string' }],
},
{
field: 'field44',
component: 'InputCountDown',
label: '验证码',
colProps: {
span: 8,
},
required: true,
},
{
field: 'field4',
component: 'Select',
label: '字段4',
colProps: {
span: 8,
},
componentProps: {
mode: 'multiple',
options: [
{
label: '选项1',
value: '1',
key: '1',
},
{
label: '选项2',
value: '2',
key: '2',
},
],
},
rules: [
2021-10-20 14:32:09 +08:00
{
2022-06-10 10:44:44 +08:00
required: true,
message: '请输入aa',
type: 'array',
2021-10-20 14:32:09 +08:00
},
2022-06-10 10:44:44 +08:00
],
},
{
field: 'field441',
component: 'Input',
label: '自定义校验',
colProps: {
span: 8,
},
rules: [
2021-10-20 14:32:09 +08:00
{
2022-06-10 10:44:44 +08:00
required: true,
// @ts-ignore
validator: async (rule, value) => {
if (!value) {
/* eslint-disable-next-line */
return Promise.reject('值不能为空');
}
if (value === '1') {
/* eslint-disable-next-line */
return Promise.reject('值不能为1');
}
return Promise.resolve();
},
trigger: 'change',
2021-10-20 14:32:09 +08:00
},
2022-06-10 10:44:44 +08:00
],
},
{
field: 'field5',
component: 'CheckboxGroup',
label: '字段5',
colProps: {
span: 8,
},
componentProps: {
options: [
{
label: '选项1',
value: '1',
},
{
label: '选项2',
value: '2',
},
],
},
rules: [{ required: true }],
},
{
field: 'field7',
component: 'RadioGroup',
label: '字段7',
colProps: {
span: 8,
},
componentProps: {
options: [
{
label: '选项1',
value: '1',
},
{
label: '选项2',
value: '2',
},
],
},
rules: [{ required: true, message: '覆盖默认生成的校验信息' }],
},
{
field: 'field8',
component: 'Input',
label: '后端异步验证',
colProps: {
span: 8,
},
helpMessage: ['本字段演示异步验证', '本地规则:必须填写', '后端规则不能包含admin'],
rules: [
2021-10-20 14:32:09 +08:00
{
2022-06-10 10:44:44 +08:00
required: true,
message: '请输入数据',
2021-10-20 14:32:09 +08:00
},
{
2022-06-10 10:44:44 +08:00
validator(_, value) {
return new Promise((resolve, reject) => {
isAccountExist(value)
.then(() => resolve())
.catch((err) => {
reject(err.message || '验证失败');
});
});
},
2021-10-20 14:32:09 +08:00
},
2022-06-10 10:44:44 +08:00
],
},
];
2021-10-20 14:32:09 +08:00
2022-06-10 10:44:44 +08:00
export default defineComponent({
components: { BasicForm, CollapseContainer, PageWrapper },
setup() {
const { createMessage } = useMessage();
const [register, { validateFields, clearValidate, getFieldsValue, resetFields, setFieldsValue }] = useForm({
labelWidth: 120,
schemas,
actionColOptions: {
span: 24,
},
});
2021-10-20 14:32:09 +08:00
2022-06-10 10:44:44 +08:00
async function validateForm() {
try {
const res = await validateFields();
console.log('passing', res);
} catch (error) {
console.log('not passing', error);
}
}
2021-10-20 14:32:09 +08:00
2022-06-10 10:44:44 +08:00
async function resetValidate() {
clearValidate();
}
2021-10-20 14:32:09 +08:00
2022-06-10 10:44:44 +08:00
function getFormValues() {
const values = getFieldsValue();
createMessage.success('values:' + JSON.stringify(values));
}
2021-10-20 14:32:09 +08:00
2022-06-10 10:44:44 +08:00
function setFormValues() {
setFieldsValue({
field1: 1111,
field5: ['1'],
field7: '1',
field33: '2020-12-12',
field3: '2020-12-12',
});
}
2021-10-20 14:32:09 +08:00
2022-06-10 10:44:44 +08:00
return {
register,
schemas,
handleSubmit: (values: any) => {
createMessage.success('click search,values:' + JSON.stringify(values));
2021-10-20 14:32:09 +08:00
},
2022-06-10 10:44:44 +08:00
getFormValues,
setFormValues,
validateForm,
resetValidate,
resetFields,
};
},
});
2021-10-20 14:32:09 +08:00
</script>