Javascript

ant-design-vue 表单验证详解

本文主要是介绍ant-design-vue 表单验证详解,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

表单验证详解

<template>
<!-- 第一个坑
:model="formState.youForm" 一定要写成这样
不要写成:model="formState"
  -->
  <a-button type="primary" @click="showHnader">显示</a-button>

  <a-form
    ref="formRef"
    :model="formState.youForm"
    :rules="rules"
    :labelCol="{ style: 'width: 100px' }"
    v-if="flag"
  >
    <a-form-item ref="youNaNe" label="姓名" name="youNaNe">
      <a-input  placeholder="请选择" v-model:value="formState.youForm.youNaNe" style="width:270px" />
    </a-form-item>

    <a-form-item label="学校" name="useSlectValue">
      <!-- formState.youForm.useSlectValue就是你选择该项的value对应的值 -->
      <a-select  placeholder="请选择"  style="width:270px" v-model:value="formState.youForm.useSlectValue">
        <a-select-option :value="item.code" 
        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
        style="width:270px"
        v-model:value="formState.youForm.date1"
        show-time
        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="formState.youForm.type">
        <a-checkbox :value="item.code" :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 style="margin-left: 10px" @click="resetForm">清空验证规则及数据</a-button>
      <a-button style="margin-left: 10px" @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:'',
          useSlectValue: undefined,
          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中的数据
      formRef.value.resetFields();
    };

    const removeResult=()=>{
       // 移除表单项的校验结果。
      formRef.value.clearValidate();
    }

    const showHnader=()=>{
      flag.value=true;
      nextTick(()=>{
        formRef.value.clearValidate();
      })
    }
    return {
      formRef,
      formState,
      rules,
      flag,
      removeResult,
      onSubmit,
      resetForm,
      showHnader
    };
  },
});
</script>
这篇关于ant-design-vue 表单验证详解的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!