ant-design-vue表单验证详解表单验证详解
<template>
<!-- 第⼀个坑
:model="uForm" ⼀定要写成这样
不要写成:model="formState"
:labelCol="{ style: 'width: 100px' }" 控制labe显⽰宽度
useSlectValue 是⼀个字符串类型的,⽽不是数组类型。否者验证规则会报错的哈
autocomplete="off" 清除input输⼊框的历史记录
:maxlength="4" 限制字符串
-->
<a-button type="primary" @click="showHnader">显⽰</a-button>
<a-form
ref="formRef"
:model="uForm"
:rules="rules"
:labelCol="{ style: 'width: 100px' }"
v-if="flag"
>
<a-form-item ref="youNaNe" label="姓名" name="youNaNe">
<a-input autocomplete="off" placeholder="请选择"
v-model:value="uNaNe"
:
maxlength="4"
/>
</a-form-item>
<a-form-item label="学校" name="useSlectValue">
<!-- uForm.useSlectValue就是你选择该项的value对应的值 -->
<a-select placeholder="请选择" v-model:value="uForm.useSlectValue">
<a-select-option :value="de"
v-for="(item,index) in formState.backDataSchool"
:key="index">
{{ item.name }}
</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="⽇期" required name="date1">
<a-date-picker
v-model:value="uForm.date1"
show-timereactive 数组
format="YYYY-MM-DD"
type="date"
valueFormat="YYYY-MM-DD"
placeholder="请选择"
/
>
</a-form-item>
<a-form-item label="爱好" name="type" >
<a-checkbox-group v-model:value="pe">
<a-checkbox :value="de" :name="item.name"
v-for="(item,index) in formState.likeBackArr"
:key="index"
>{{ item.name }}</a-checkbox>
</a-checkbox-group>
</a-form-item>
<a-form-item :wrapper-col="{ span: 14, offset: 1 }">
<a-button type="primary" @click="onSubmit">提交</a-button>
<a-button @click="resetForm">清空验证规则及数据</a-button>
<a-button @click="removeResult">移除表单项的校验结果</a-button>
</a-form-item>
</a-form>
</template>
<script lang="ts">
import { defineComponent, reactive, ref, toRaw, nextTick } from 'vue';
export default defineComponent({
setup() {
const formRef = ref();
const flag=ref(false)
const formState= reactive({
youForm:{
youNaNe:'',
//这个时候就会默认[第⼀⼩学],
//他是通过code去寻对应的name值,然后展⽰在视图上哈
// 需要注意的是,它选中的值是⼀个字符串;
// ⽽不是数组类型的 ['001']这样是会出问题的虽然可以回填,但是验证规则会出问题的哈 useSlectValue: '001',
date1: undefined,
delivery: false,
type: [],
},
backDataSchool:[
{
name:'第⼀⼩学',code:'001'
},
{
name:'第⼆⼩学',code:'002'
},
],
likeBackArr:[
{name:'睡觉',code:'1'},
{name:'吃饭',code:'2'},
{name:'吃饭',code:'3'},
]
});
const rules = {
youNaNe: [
{ required: true, message: '请输⼊姓名', trigger: 'blur' },
],
useSlectValue: [{ required: true, message: '请选择学校', trigger: 'change' }],
date1: [{ required: true, message: '请选择⽇期', trigger: 'change', }],
// date1: [{ required: true, message: '请选择⽇期', trigger: 'change', type: 'object' }],
/
/ 如果 type: 'object',需要注意的是你需要定义⼀个接⼝
type: [
{
type: 'array',
required: true,
message: '请选择',
trigger: 'change',
},
],
};
const onSubmit = () => {
formRef.value
.validate()
.then(() => {
console.log('values', formState, toRaw(formState));
})
.catch((err:any) => {
console.log('err')
});
};
const resetForm = () => {
// 移除规则和重置data中的数据
setFields();
};
const removeResult=()=>{
// 移除表单项的校验结果。
formRef.value.clearValidate();
}
const showHnader=()=>{
flag.value=true;
nextTick(()=>{
formRef.value.clearValidate();
})
}
return {
formRef,
formState,
rules,
flag,
removeResult,
onSubmit,
resetForm,
showHnader
};
},
});
</script>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论