粘貼可用的element-ui validateField局部校驗(yàn)
正文
element-ui的表單提交基本每個(gè)項(xiàng)目都會(huì)使用到,其中針對(duì)特定區(qū)域的值進(jìn)行校驗(yàn)的場(chǎng)景也不少,其文檔相對(duì)簡(jiǎn)單,下面就貼上代碼,復(fù)制粘貼到一個(gè).vue文件即可運(yùn)行。
場(chǎng)景:人員信息錄入時(shí),表單可以就其中某一行進(jìn)行保存,也可創(chuàng)建多個(gè)后批量保存,保存時(shí)如果信息不全會(huì)只針對(duì)當(dāng)前行進(jìn)行校驗(yàn)。

1、template
<template>
<el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="80px">
<div
v-for="(domain, index) in dynamicValidateForm.domains"
:key="index"
class="form-item"
>
<el-form-item>
{{ index + 1 }}
</el-form-item>
<el-form-item
label="姓名"
size="mini"
:prop="'domains.' + index + '.name'"
:rules="{
required: true,
message: '姓名不能為空',
trigger: 'blur',
}"
>
<el-input v-model="domain.name"></el-input>
</el-form-item>
<el-form-item
label="年齡"
size="mini"
:prop="'domains.' + index + '.age'"
:rules="{
required: true,
message: '年齡不能為空',
trigger: 'blur',
}"
>
<el-input v-model="domain.age"></el-input>
</el-form-item>
<el-form-item
label="地址"
size="mini"
:prop="'domains.' + index + '.address'"
:rules="{
required: true,
message: '地址不能為空',
trigger: 'blur',
}"
>
<el-input v-model="domain.address"></el-input>
</el-form-item>
<el-form-item
label="手機(jī)"
size="mini"
:prop="'domains.' + index + '.phone'"
:rules="{
required: true,
message: '手機(jī)不能為空',
trigger: 'blur',
}"
>
<el-input v-model="domain.phone"></el-input>
</el-form-item>
<el-form-item>
<el-button size="mini" type="primary" @click="saveCurrentData(domain, index)"
>保存</el-button
>
</el-form-item>
</div>
<el-form-item class="form-footer">
<el-button type="primary" size="mini" @click="submitForm('dynamicValidateForm')"
>全部保存</el-button
>
<el-button size="mini" @click="resetForm('dynamicValidateForm')">重置</el-button>
</el-form-item>
</el-form>
</template>
2、script
<script>
export default {
data() {
return {
dynamicValidateForm: {
domains: [],
},
};
},
created() {
const item = {
name: "",
age: "",
address: "",
phone: "",
};
for (let i = 0; i < 5; i++) {
this.dynamicValidateForm.domains.push(Object.assign({}, item));
}
},
methods: {
// 定義局部校驗(yàn)函數(shù)
validateFn(domain, index) {
let validateFieldArr = ["name", "age", "address", "phone"];
return new Promise((resolve, reject) => {
for (let item of validateFieldArr) {
this.$refs.dynamicValidateForm.validateField(
`domains.${index}.${item}`,
(err) => {
if (err) {
return reject(err); // 失敗返回err
}
resolve(true); // 成功返回true
}
);
}
});
},
async saveCurrentData(domain, index) {
const validateResult = await this.validateFn(domain, index);
if (validateResult) {
console.log("-------校驗(yàn)通過(guò)啦------");
}
},
submitForm(formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert("submit!");
} else {
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
},
},
};
</script>
3、style
<style>
.form-item {
display: flex;
}
.form-footer {
display: flex;
justify-content: center;
}
</style>以上就是粘貼可用element-ui中validateField局部校驗(yàn)的詳細(xì)內(nèi)容,更多關(guān)于element-ui validateField局部校驗(yàn)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
mpvue跳轉(zhuǎn)頁(yè)面及注意事項(xiàng)
這篇文章主要介紹了mpvue跳轉(zhuǎn)頁(yè)面及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2018-08-08
Vue框架+Element-ui(el-upload組件)使用http-request方法上傳文件并顯示文件上傳進(jìn)度功能
這篇文章主要介紹了Vue框架+Element-ui(el-upload組件)使用http-request方法上傳文件并顯示文件上傳進(jìn)度功能,本通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08
vue結(jié)合axios實(shí)現(xiàn)restful風(fēng)格的四種請(qǐng)求方式
這篇文章主要介紹了vue結(jié)合axios實(shí)現(xiàn)restful風(fēng)格的四種請(qǐng)求方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Vue 頁(yè)面切換效果之 BubbleTransition(推薦)
使用 vue,vue-router,animejs 來(lái)講解如何實(shí)現(xiàn)vue頁(yè)面切換效果之 BubbleTransition,需要的朋友參考下吧2018-04-04
VUE配置proxy代理的開(kāi)發(fā)測(cè)試及生產(chǎn)環(huán)境
這篇文章主要為大家介紹了VUE配置proxy代理的開(kāi)發(fā)環(huán)境、測(cè)試環(huán)境、生產(chǎn)環(huán)境詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

