欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

elementUI動(dòng)態(tài)嵌套el-form表單校驗(yàn)舉例詳解

 更新時(shí)間:2023年06月02日 09:12:47   作者:小綿楊Yancy  
最近工作遇到個(gè)需求,表單可以進(jìn)行增加刪除操作,需要進(jìn)行表單校驗(yàn),這篇文章主要給大家介紹了關(guān)于elementUI動(dòng)態(tài)嵌套el-form表單校驗(yàn)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下

一、基礎(chǔ)表單校驗(yàn)

前端項(xiàng)目開發(fā)過程中表單校驗(yàn)是非常常見的需求,elementUI的el-form組件也是支持配置rules屬性來配置表單項(xiàng)的校驗(yàn)。

Form 組件允許你驗(yàn)證用戶的輸入是否符合規(guī)范,來幫助你找到和糾正錯(cuò)誤。
Form 組件提供了表單驗(yàn)證的功能,只需為 rules 屬性傳入約定的驗(yàn)證規(guī)則,并將 form-Item 的 prop 屬性設(shè)置為需要驗(yàn)證的特殊鍵值即可。

例如:

不要漏掉el-from-item的prop屬性,需要和rules對象中的屬性字段對應(yīng)。

定義rules對象:

提交表單時(shí)驗(yàn)證規(guī)則:

校驗(yàn)是通過調(diào)用表單實(shí)例(表單組件上的ref屬性綁定的變量)上的validate方法來實(shí)現(xiàn)的(異步)

這樣,在我們進(jìn)行驗(yàn)證時(shí),如果表單項(xiàng)不符合rules中的規(guī)則,那么就會(huì)在對應(yīng)的表單項(xiàng)下面提示我們設(shè)定的提示信息。

校驗(yàn)規(guī)則

elementUI中el-from校驗(yàn)的規(guī)則是引用的https://github.com/yiminghe/async-validator

比較常用的規(guī)則屬性有:

必填:required

可以賦值一個(gè)boolean值,true表示必填。

類型:type

賦值有一個(gè)字符,表示要驗(yàn)證的內(nèi)容的類型:string、number等,詳情見下圖:

正則驗(yàn)證:pattern

如果對于type無法驗(yàn)證的值,我們可以使用pattern賦值一個(gè)正則表達(dá)式來更加靈活的驗(yàn)證規(guī)則。

驗(yàn)證前轉(zhuǎn)換:transform

有時(shí)有必要在驗(yàn)證之前轉(zhuǎn)換一個(gè)值,可以使用tansform屬性,是一個(gè)函數(shù),參數(shù)是要驗(yàn)證的值,該函數(shù)的返回值是處理后再驗(yàn)證的值。

例如上面代碼name的校驗(yàn)中就用到了這幾個(gè)屬性,其中transform會(huì)將用戶輸入的內(nèi)容去掉字符串首位空格(trim方法)后再進(jìn)行其他規(guī)則驗(yàn)證。

 name: {
    type: 'string',
    required: true,
    pattern: /^[a-z]+$/,
    transform(value) {
      return value.trim();
    },
 }

二、動(dòng)態(tài)嵌套表單校驗(yàn)

了解了基礎(chǔ)的表單校驗(yàn),那么有的時(shí)候會(huì)遇到比較復(fù)雜的表單對象,并且可能需要?jiǎng)討B(tài)增加表單項(xiàng)。

elementUI官網(wǎng)給出了一個(gè)比較簡單的例子:添加/刪除表單項(xiàng)

在此基礎(chǔ)上,我增加了一下表單對象的復(fù)雜度。

可以看到,domains數(shù)組每個(gè)對象下包含了info對象屬性,info對象下有name和intro屬性,是一個(gè)嵌套的對象,并且我們可以動(dòng)態(tài)的新增和刪除domains中的對象,那么在表單中該如何驗(yàn)證呢?

本質(zhì)上,檢驗(yàn)是通過設(shè)置的rules對象中的屬性,去匹配表單項(xiàng)上設(shè)置的prop,然后對雙向綁定的內(nèi)容進(jìn)行驗(yàn)證。

首先,我們設(shè)置rules對象:

直接按照表單對象的層級(jí)設(shè)置屬性即可,因?yàn)閐omains數(shù)組中的對象肯定是公用一套規(guī)則的。

可以看到,我們直接循環(huán)domains數(shù)組,然后通過設(shè)置prop="'domains.' + index + '.value'"來實(shí)現(xiàn)數(shù)組中的每個(gè)對象獨(dú)立校驗(yàn)。
接著我們還要在每個(gè)表單項(xiàng)el-form-item上定義對應(yīng)的rules,就能達(dá)到新增的表單項(xiàng)也能夠校驗(yàn)的目的了。

效果:

三、示例源碼

代碼基于vue3 + ts,項(xiàng)目中如果安裝并注冊了elemetPlus,可以直接cv查看效果。

<template>
    <div class="form-box">
        <el-form ref="formRef" :model="dynamicValidateForm" label-width="120px" :rules="rules">
            <div class="inner-form-box">
                <el-form-item prop="email" label="Email??">
                    <el-input v-model="dynamicValidateForm.email" />
                </el-form-item>
            </div>
            <div v-for="(domain, index) in dynamicValidateForm.domains" :key="domain.key" class="inner-form-box">
                <span>{{ `網(wǎng)站${index + 1}??:` }}</span>
                <el-form-item label="網(wǎng)站域名:" :prop="'domains.' + index + '.value'" :rules="rules.domains.value">
                    <el-input v-model="domain.value" />
                </el-form-item>
                <div>
                    <span>{{ `網(wǎng)站${index + 1}詳情??:` }}</span>
                    <el-form-item label="網(wǎng)站名稱:" :prop="'domains.' + index + '.info.name'" :rules="rules.domains.info.name">
                        <el-input v-model="domain.info.name" />
                    </el-form-item>
                    <el-form-item label="網(wǎng)站簡介:" :prop="'domains.' + index + '.info.intro'"
                        :rules="rules.domains.info.intro">
                        <el-input v-model="domain.info.intro" />
                    </el-form-item>
                </div>
                <div class="del-btn-box">
                    <el-button type="danger" link @click="removeDomain(domain)">刪除</el-button>
                </div>
            </div>
            <el-form-item>
                <el-button type="primary" @click="submitForm(formRef)">Submit</el-button>
                <el-button @click="addDomain">New domain</el-button>
                <el-button @click="resetForm(formRef)">Reset</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'
const formRef = ref<FormInstance>()
const dynamicValidateForm = reactive<{
    domains: DomainItem[]
    email: string
}>({
    domains: [
        {
            key: 1,
            value: '',
            info: {
                name: '',
                intro: ''
            }
        },
    ],
    email: '',
})
const rules = {
    email: [
        {required: true,message: '郵箱地址不能為空哦',trigger: 'blur',},
        {type: 'email',message: '請輸入正確的郵箱格式',trigger: ['blur', 'change'],},
    ],
    domains: {
        value: {required: true,message: '網(wǎng)站域名不能為空哦',trigger: 'blur',},
        info: {
            name: {required: true,message: '網(wǎng)站名稱不能為空哦',trigger: 'blur',},
            intro: {required: true,message: '網(wǎng)站簡介不能為空哦',trigger: 'blur',}
        }
    }
}
interface DomainItem {
    key: number
    value: string
    info: {
        name: string
        intro: string
    }
}
const removeDomain = (item: DomainItem) => {
    const index = dynamicValidateForm.domains.indexOf(item)
    if (index !== -1) {
        dynamicValidateForm.domains.splice(index, 1)
    }
}
const addDomain = () => {
    dynamicValidateForm.domains.push({
        key: Date.now(),
        value: '',
        info: {
            name: '',
            intro: ''
        }
    })
}
const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            console.log(dynamicValidateForm)
        } else {
            console.log('error submit!')
            return false
        }
    })
}
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}
</script>
<style scoped>
.form-box {
    padding: 50px;
}
.inner-form-box {
    position: relative;
    padding: 10px;
    margin-bottom: 10px;
    box-shadow: 0px 0px 5px 0px #f2f2f2;
}
.del-btn-box {
    position: absolute;
    right: 0;
    top: 0;
    transform: translateX(120%);
}
</style>

總結(jié)

到此這篇關(guān)于elementUI動(dòng)態(tài)嵌套el-form表單校驗(yàn)的文章就介紹到這了,更多相關(guān)elementUI動(dòng)態(tài)嵌套表單校驗(yàn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue中的directive指令快速使用

    Vue中的directive指令快速使用

    這篇文章主要介紹了Vue中的directive指令快速使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 淺析vue深復(fù)制

    淺析vue深復(fù)制

    這篇文章主要介紹了vue深復(fù)制的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下
    2018-01-01
  • 如何使用elementUI組件實(shí)現(xiàn)表格的分頁及搜索功能

    如何使用elementUI組件實(shí)現(xiàn)表格的分頁及搜索功能

    最近在使用element-ui的表格組件時(shí),遇到了搜索框功能的實(shí)現(xiàn)問題,這篇文章主要給大家介紹了關(guān)于如何使用elementUI組件實(shí)現(xiàn)表格的分頁及搜索功能的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • Vue生命周期activated之返回上一頁不重新請求數(shù)據(jù)操作

    Vue生命周期activated之返回上一頁不重新請求數(shù)據(jù)操作

    這篇文章主要介紹了Vue生命周期activated之返回上一頁不重新請求數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 解決vue2使用腳手架配置prettier報(bào)錯(cuò)prettier/prettier:context.getPhysicalFilename is not a function

    解決vue2使用腳手架配置prettier報(bào)錯(cuò)prettier/prettier:context.getPhysical

    這篇文章主要介紹了解決vue2使用腳手架配置prettier報(bào)錯(cuò)prettier/prettier:context.getPhysicalFilename is not a function問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue 監(jiān)聽鍵盤回車事件詳解 @keyup.enter || @keyup.enter.native

    vue 監(jiān)聽鍵盤回車事件詳解 @keyup.enter || @keyup.enter.native

    今天小編就為大家分享一篇vue 監(jiān)聽鍵盤回車事件詳解 @keyup.enter || @keyup.enter.native,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 基于vue手寫tree插件的那點(diǎn)事兒

    基于vue手寫tree插件的那點(diǎn)事兒

    這篇文章主要給大家介紹了基于vue手寫tree插件的那點(diǎn)事兒,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • vue 實(shí)現(xiàn)可拖曳的樹狀結(jié)構(gòu)圖

    vue 實(shí)現(xiàn)可拖曳的樹狀結(jié)構(gòu)圖

    這篇文章主要介紹了vue 實(shí)現(xiàn)可拖曳的樹狀結(jié)構(gòu)圖,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下
    2021-04-04
  • 使用Vite2+Vue3渲染Markdown文檔的方法實(shí)踐

    使用Vite2+Vue3渲染Markdown文檔的方法實(shí)踐

    本文主要介紹了Vite2+Vue3渲染Markdown文檔的方法實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下
    2021-08-08
  • 如何正確理解vue中的key詳解

    如何正確理解vue中的key詳解

    這篇文章主要給大家介紹了關(guān)于如何正確理解vue中key的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11

最新評(píng)論