使用ElementUI循環(huán)生成的Form表單添加校驗
ElementUI循環(huán)生成的Form表單添加校驗
ElementUI 中使用循環(huán)生成的form表單需要動態(tài)添加校驗的規(guī)則
在data中定義好需要的rule,使用Element的語法給 :rules 動態(tài)綁定
:prop="getAllTableData.${i}.value"getAllTableData是v-for綁定的數(shù)組,i是索引,value是表單綁定的v-model的名稱
HTML代碼片段
// An highlighted block
<!-- dialog -->
<el-dialog
? ? :title="dialogTitle"
? ? :visible.sync="dialogFormVisible"
? ? @close="dialogClose"
? ? :close-on-click-modal="false"
? ? width="40%"
? ? class="dialogForm-box"
>
? ? <el-form
? ? ? ? label-position="left"
? ? ? ? label-width="120px"
? ? ? ? ref="dialogRef"
? ? ? ? :model="getAllFormData"
? ? >
? ? ?? ?<!-- 循環(huán)開始 ?-->
? ? ? ? <template v-for="(item,i) in getAllFormData.getAllTableData">
? ? ? ? ?? ?<!--?
? ? ? ? ?? ?判斷循環(huán)中的元素是否需要下拉框 ?
? ? ? ? ?? ?:rules="item.rule" ?循環(huán)中的數(shù)據(jù) 自定義的 rule 規(guī)則
? ? ? ? ?? ?:prop="`getAllTableData.${i}.value`"
? ? ? ? ?? ?每次循環(huán)的時候進(jìn)行取值, 相當(dāng)于給每個form綁定了唯一的model
? ? ? ? ?? ?-->
? ? ? ? ? ? <el-form-item
? ? ? ? ? ? ? ? v-if="item.selectFlag"
? ? ? ? ? ? ? ? :key="item.labelName"
? ? ? ? ? ? ? ? :label="item.labelName"
? ? ? ? ? ? ? ? :prop="`getAllTableData.${i}.value`"
? ? ? ? ? ? ? ? :rules="item.rule"
? ? ? ? ? ? ? ? class="selectInput-box"
? ? ? ? ? ? >
? ? ? ? ? ? ? ? <el-select v-model="item.value" placeholder="請選擇">
? ? ? ? ? ? ? ? ? ? <el-option
? ? ? ? ? ? ? ? ? ? ? ? v-for="(item1) in item.selectOption"
? ? ? ? ? ? ? ? ? ? ? ? :key="item1.value"
? ? ? ? ? ? ? ? ? ? ? ? :label="item1.label"
? ? ? ? ? ? ? ? ? ? ? ? :value="item1.value"
? ? ? ? ? ? ? ? ? ? ></el-option>
? ? ? ? ? ? ? ? </el-select>
? ? ? ? ? ? </el-form-item>
? ? ? ? ? ? <el-form-item
? ? ? ? ? ? ? ? v-else-if="!item.hidden && item.disabled"
? ? ? ? ? ? ? ? :key="item.labelName"
? ? ? ? ? ? ? ? :prop="`getAllTableData.${i}.value`"
? ? ? ? ? ? ? ? :rules="item.rule"
? ? ? ? ? ? ? ? :label="item.labelName"
? ? ? ? ? ? >
? ? ? ? ? ? ? ? <el-input
? ? ? ? ? ? ? ? ? ? v-model="item.value"
? ? ? ? ? ? ? ? ? ? clearable
? ? ? ? ? ? ? ? ? ? autocomplete="off"
? ? ? ? ? ? ? ? ? ? :disabled="true"
? ? ? ? ? ? ? ? ></el-input>
? ? ? ? ? ? </el-form-item>
? ? ? ? ? ? <el-form-item
? ? ? ? ? ? ? ? v-else-if="!item.hidden"
? ? ? ? ? ? ? ? :key="i"
? ? ? ? ? ? ? ? :prop="`getAllTableData.${i}.value`"
? ? ? ? ? ? ? ? :rules="item.rule"
? ? ? ? ? ? ? ? :label="item.labelName"
? ? ? ? ? ? >
? ? ? ? ? ? ? ? <el-input v-model="item.value" clearable autocomplete="off"></el-input>
? ? ? ? ? ? </el-form-item>
? ? ? ? </template>
? ? </el-form>
? ? <div slot="footer" class="dialog-footer">
? ? ? ? <el-button @click="dialogFormVisible = false">取 消</el-button>
? ? ? ? <el-button type="primary" @click="FormSureBtn" :loading="SureBtnLoading">確 定</el-button>
? ? </div>
</el-dialog>VUE中data中的聲明
getAllFormData: {
?? ??? ?getAllTableData: [
?? ??? ? ? ? {
?? ??? ? ? ? ? ? labelName: "規(guī)則ID",
?? ??? ? ? ? ? ? value: "",
?? ??? ? ? ? ? ? propName: "ruleid",
?? ??? ? ? ? ? ? disabled: true,
?? ??? ? ? ? ? ? hidden: true,
?? ??? ? ? ? ? ? // 規(guī)則必須也得定義在form綁定的model中
?? ??? ? ? ? ? ? rule: {
?? ??? ? ? ? ? ? ? ? required: false
?? ??? ? ? ? ? ? }
?? ??? ? ? ? },
?? ??? ? ? ? {
?? ??? ? ? ? ? ? labelName: "編碼",
?? ??? ? ? ? ? ? value: "",
?? ??? ? ? ? ? ? propName: "code",
?? ??? ? ? ? ? ? rule: {
?? ??? ? ? ? ? ? ?? ?// ElementUI 表單校驗規(guī)則的語法
?? ??? ? ? ? ? ? ? ? validator: (rule, value, callback) => {
?? ??? ? ? ? ? ? ? ? ? ? if (value == "") {
?? ??? ? ? ? ? ? ? ? ? ? ? ? callback();
?? ??? ? ? ? ? ? ? ? ? ? } else {
?? ??? ? ? ? ? ? ? ? ? ? ?? ?// 采用正則表達(dá)式進(jìn)行判斷
?? ??? ? ? ? ? ? ? ? ? ? ? ? let reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/;
?? ??? ? ? ? ? ? ? ? ? ? ? ? if (reg.test(value)) {
?? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? callback(new Error("請輸入英文或數(shù)字"));
?? ??? ? ? ? ? ? ? ? ? ? ? ? } else {
?? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? callback();
?? ??? ? ? ? ? ? ? ? ? ? ? ? }
?? ??? ? ? ? ? ? ? ? ? ? }
?? ??? ? ? ? ? ? ? ? },
?? ??? ? ? ? ? ? ? ? trigger: "blur"
?? ??? ? ? ? ? ? }
?? ??? ? ? ? },
?? ??? ? ? ? {
?? ??? ? ? ? ? ? labelName: "名稱",
?? ??? ? ? ? ? ? value: "",
?? ??? ? ? ? ? ? propName: "name",
?? ??? ? ? ? ? ? rule: {}
?? ??? ? ? ? },
?? ??? ? ? ? {
?? ??? ? ? ? ? ? labelName: "審核類別",
?? ??? ? ? ? ? ? value: "",
?? ??? ? ? ? ? ? propName: "typename",
?? ??? ? ? ? ? ? rule: {}
?? ??? ? ? ? },
?? ??? ? ? ? {
?? ??? ? ? ? ? ? labelName: "字段名稱",
?? ??? ? ? ? ? ? value: "",
?? ??? ? ? ? ? ? propName: "fieldcode",
?? ??? ? ? ? ? ? rule: {}
?? ??? ? ? ? },
?? ??? ? ? ? {
?? ??? ? ? ? ? ? labelName: "備注",
?? ??? ? ? ? ? ? value: "",
?? ??? ? ? ? ? ? propName: "memo",
?? ??? ? ? ? ? ? rule: {}
?? ??? ? ? ? },
?? ??? ?
?? ??? ? ? ? {
?? ??? ? ? ? ? ? labelName: "評分",
?? ??? ? ? ? ? ? value: "",
?? ??? ? ? ? ? ? propName: "mark",
?? ??? ? ? ? ? ? prop: "mark",
?? ??? ? ? ? ? ? rule: {
?? ??? ? ? ? ? ? ? ? required: true,
?? ??? ? ? ? ? ? ? ? validator: (rule, value, callback) => {
?? ??? ? ? ? ? ? ? ? ? ? if (value == "") {
?? ??? ? ? ? ? ? ? ? ? ? ? ? callback(new Error("請輸入數(shù)字"));
?? ??? ? ? ? ? ? ? ? ? ? } else {
?? ??? ? ? ? ? ? ? ? ? ? ? ? let reg = /^[0-9]+([.]{1}[0-9]{1})?$/;
?? ??? ? ? ? ? ? ? ? ? ? ? ? if (!reg.test(value) || value > 100) {
?? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? callback(new Error("請輸入0-100的有效數(shù)字,可保留一位小數(shù)"));
?? ??? ? ? ? ? ? ? ? ? ? ? ? } else {
?? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? callback();
?? ??? ? ? ? ? ? ? ? ? ? ? ? }
?? ??? ? ? ? ? ? ? ? ? ? }
?? ??? ? ? ? ? ? ? ? },
?? ??? ? ? ? ? ? ? ? trigger: "blur"
?? ??? ? ? ? ? ? }
?? ??? ? ? ? },
?? ??? ? ? ? {
?? ??? ? ? ? ? ? labelName: "警告標(biāo)識",
?? ??? ? ? ? ? ? value: "",
?? ??? ? ? ? ? ? propName: "warnsign",
?? ??? ? ? ? ? ? prop: "warnsign",
?? ??? ? ? ? ? ? selectFlag: true,
?? ??? ? ? ? ? ? selectOption: [
?? ??? ? ? ? ? ? ? ? {
?? ??? ? ? ? ? ? ? ? ? ? value: "0",
?? ??? ? ? ? ? ? ? ? ? ? label: "否"
?? ??? ? ? ? ? ? ? ? },
?? ??? ? ? ? ? ? ? ? {
?? ??? ? ? ? ? ? ? ? ? ? value: "1",
?? ??? ? ? ? ? ? ? ? ? ? label: "是"
?? ??? ? ? ? ? ? ? ? }
?? ??? ? ? ? ? ? ],
?? ??? ? ? ? ? ? rule: {
?? ??? ? ? ? ? ? ? ? required: true,
?? ??? ? ? ? ? ? ? ? message: "請選擇對應(yīng)的選項"
?? ??? ? ? ? ? ? }
?? ??? ? ? ? }
?? ??? ? ?? ?]
?},效果圖

required: true,說明該內(nèi)容是必須填寫的
正則表達(dá)式可以自定義項目需求的樣式
ElementUI循環(huán)動態(tài)生成表單校驗問題

<el-form :model="upsertForm" ref="upsertForm" label-width="100px" class="demo-dynamic">
<el-form-item
prop="email"
label="郵箱"
:rules="[
{ required: true, message: '請輸入郵箱地址', trigger: 'blur' },
{ type: 'email', message: '請輸入正確的郵箱地址', trigger: ['blur', 'change'] }
]"
>
<el-input v-model="upsertForm.email"></el-input>
</el-form-item>
<el-form-item
v-for="(domain, index) in upsertForm.domains"
:label="'專利號' + index"
:key="domain.key"
:prop="'domains.' + index + '.value'"
:rules="{
required: true, message: '專利號不能為空', trigger: 'blur'
}"
>
<el-input v-model="domain.value"></el-input><el-button @click.prevent="removeDomain(domain)">刪除</el-button>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('upsertForm')">提交</el-button>
<el-button @click="addDomain">新增專利號</el-button>
<el-button @click="resetForm('upsertForm')">重置</el-button>
</el-form-item>
</el-form>
<script>
export default {
data() {
return {
upsertForm: {
domains: [{
value: ''
}],
email: ''
}
};
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
},
removeDomain(item) {
var index = this.upsertForm.domains.indexOf(item)
if (index !== -1) {
this.upsertForm.domains.splice(index, 1)
}
},
addDomain() {
this.upsertForm.domains.push({
value: '',
key: Date.now()
});
}
}
}
</script>注意:循環(huán)的是文本框,它隸屬于form 表單,form 表單綁定的是upsertForm ,所以,它的對象必須要在upsertForm 里面,才能生效
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- elementui之el-table如何通過v-if控制按鈕顯示與隱藏
- Elementui?el-input輸入框校驗及表單校驗實例代碼
- elementUI動態(tài)嵌套el-form表單校驗舉例詳解
- Vue?elementUI表單嵌套表格并對每行進(jìn)行校驗詳解
- vue elementUI 表單校驗的實現(xiàn)代碼(多層嵌套)
- vue elementUI 表單校驗功能之?dāng)?shù)組多層嵌套
- 基于Vue+elementUI實現(xiàn)動態(tài)表單的校驗功能(根據(jù)條件動態(tài)切換校驗格式)
- v-if 導(dǎo)致 elementui 表單校驗失效問題解決方案
相關(guān)文章
Vue-cli3執(zhí)行serve和build命令時nodejs內(nèi)存溢出問題及解決
這篇文章主要介紹了Vue-cli3執(zhí)行serve和build命令時nodejs內(nèi)存溢出問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
Vue組件庫ElementUI實現(xiàn)表格加載樹形數(shù)據(jù)教程
這篇文章主要為大家詳細(xì)介紹了Vue組件庫ElementUI實現(xiàn)表格加載樹形數(shù)據(jù)教程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
詳解vue中的動態(tài)組件component和keep-alive
這篇文章主要介紹了詳解vue中的動態(tài)組件component和keep-alive的相關(guān)資料,這大家需要注意include屬性和exclude屬性只能用一個,不能同時使用,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11
Vue配合iView實現(xiàn)省市二級聯(lián)動的示例代碼
本篇文章主要介紹了Vue配合iView實現(xiàn)省市二級聯(lián)動的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
vue+elementUI多表單同時提交及表單校驗最新解決方案
假設(shè)有一個頁面,需要分三步填寫三個表單,且每個表單位于獨立的組件中,然后最后保存同時提交,如何進(jìn)行表單必填項校驗,下面小編給大家介紹vue+elementUI多表單同時提交及表單校驗最新解決方案,感興趣的朋友一起看看吧2024-03-03

