2021-5-27 前端達(dá)人
一個(gè)前端小菜雞。若下邊的內(nèi)容有瑕希望告訴我,如果有更好的方法希望告訴我,感謝萬(wàn)分。
這篇文章主要介紹的對(duì)el-upload放在表單中提交之前rules的驗(yàn)證。這里的圖片是必須提交項(xiàng)如果可以不提交可用常用方法直接提交就可以。
<el-form ref="personform" label-position="right" label-width="120px" :model="formLabelAlign" status-icon :rules="rules"> <el-row> <el-form-item label="簡(jiǎn)述"> <el-input type="textarea" v-model="formLabelAlign.paper" autocomplete="off"></el-input> </el-form-item> </el-row> <el-row> <el-form-item prop="file" ref="uploadpic"> <el-upload
style="display:inline-block" :limit="2" class="upload-demo" ref="upload" action="/hqx/knowledge/importKnowledge" :before-upload="beforeAvatarUpload" :auto-upload="false" :on-change="imageChange" :on-remove="imageRemove" > <el-button slot="trigger" size="small" type="primary" plain>上傳</el-button> </el-upload> </el-form-item> </el-row> </el-form> <script> import _ from "lodash"; export default { data() { return { xiugai: false, formLabelAlign: { paper: "" }, images: [],// 圖片存儲(chǔ) rules: { file: [{ required: true, message: "請(qǐng)上傳圖片", trigger: "change" }] }, haspic: false // 默認(rèn)沒(méi)有傳圖片 }; }, methods: { beforeAvatarUpload(file) { // 文件類型進(jìn)行判斷 const isJPG = /^image\/(jpeg|png|jpg)$/.test(file.type); const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG) { this.$message.error("上傳圖片只能是 image/jpeg/png 格式!"); } if (!isLt2M) { this.$message.error("上傳圖片大小不能超過(guò) 2MB!"); } return isJPG && isLt2M; }, imageChange(file, fileList, name) {//on-change觸發(fā) this.images["file"] = fileList; this.haspic = true; // 如果上傳了就不顯示提示圖片警告 if (typeof this.images.file != "undefined") { if (this.images.file.length > 0) { this.$refs["uploadpic"].clearValidate(); } } }, imageRemove(file, fileList, name) { //on-remove觸發(fā) //如果images為空了說(shuō)明并沒(méi)有提交圖片所以需要顯示警告 if (typeof this.images.file != "undefined") { if (this.images.file.length > 0) { this.$refs["uploadpic"].clearValidate(); } else { this.$refs["uploadpic"].validate(); this.haspic = false; } } }, confirm() {// 提交綁定的事件 if (this.haspic) { // 去掉rules中對(duì)圖片的驗(yàn)證 _.unset(this.rules, ["file"]); this.$refs["personform"].validate(valid => { if (valid) { console.log("說(shuō)明已經(jīng)添加了圖片直接提交就行了"); const wfForm = new FormData(); wfForm.append( 'dsc',this.formLabelAlign.paper) Object.entries(this.images).forEach(file => { file[1].forEach(item => { wfForm.append('files', item.raw) wfForm.append(item.name, file[0]) }) }) // 直接提交 } else { console.log("error submit!!"); return false; } }); } else { // 向rules提價(jià)一條對(duì)圖片的驗(yàn)證。 _.set(this.rules, "file", { required: true, message: "請(qǐng)上傳圖片", trigger: "change"}); this.$refs["personform"].validate(valid => { if (valid) { console.log("說(shuō)明圖片沒(méi)有提交"); } else { console.log("error submit!!"); return false; } }); } } } }; </script>
下邊解釋一下每段代碼的含義:
1.
imageChange(file, fileList, name) {//on-change觸發(fā) this.images["file"] = fileList; this.haspic = true; // 如果上傳了就不顯示提示圖片警告 if (typeof this.images.file != "undefined") { if (this.images.file.length > 0) { this.$refs["uploadpic"].clearValidate(); } } }
if (typeof this.images.file != “undefined”) 這個(gè)可加可不加
其中的一個(gè)原因是因?yàn)橐l繁對(duì)rules進(jìn)行操作因?yàn)閑lement的el-upload的提示功能在選擇了圖片的時(shí)候并不會(huì)對(duì)圖片的提示進(jìn)行更改所以只能自己進(jìn)行操作更改他顯示或者隱藏
haspic是用來(lái)記錄他是否上傳了圖片 如果上傳為true否則為false 在后面提交的時(shí)候有用。
2.考慮到用戶可能會(huì)選擇了圖片又刪除了所以加上了一個(gè)判斷
如果在提交的時(shí)候進(jìn)行驗(yàn)證或者不考慮用戶全部刪除顯示提示可不加
imageRemove(file, fileList, name) { //on-remove觸發(fā) //如果images為空了說(shuō)明并沒(méi)有提交圖片所以需要顯示警告 if (typeof this.images.file != "undefined") { if (this.images.file.length > 0) { this.$refs["uploadpic"].clearValidate(); } else { this.$refs["uploadpic"].validate(); this.haspic = false; } } },
confirm() {// 提交綁定的事件 if (this.haspic) { // 去掉rules中對(duì)圖片的驗(yàn)證 _.unset(this.rules, ["file"]); this.$refs["personform"].validate(valid => { if (valid) { console.log("說(shuō)明已經(jīng)添加了圖片直接提交就行了"); const wfForm = new FormData(); wfForm.append( 'dsc',this.formLabelAlign.paper) Object.entries(this.images).forEach(file => { file[1].forEach(item => { wfForm.append('files', item.raw) wfForm.append(item.name, file[0]) }) }) // 直接提交 } else { console.log("error submit!!"); return false; } }); } else { // 向rules提價(jià)一條對(duì)圖片的驗(yàn)證。 _.set(this.rules, "file", { required: true, message: "請(qǐng)上傳圖片", trigger: "change"}); this.$refs["personform"].validate(valid => { if (valid) { console.log("說(shuō)明圖片沒(méi)有提交"); } else { console.log("error submit!!"); return false; } }); } } } };
提交的時(shí)候進(jìn)行判斷。因?yàn)闆](méi)有想到其他的方法所以寫(xiě)了一個(gè)變量判斷是否在rules加上對(duì)圖片的判斷。因?yàn)槿绻嬖趯?duì)圖片的判斷。form驗(yàn)證的時(shí)候就總是throw error 不能進(jìn)行提交操作this.$refs[“personform”].validate(valid){}是提交form表單的時(shí)的驗(yàn)證
(1)在有圖片的時(shí)候去掉對(duì)圖片的驗(yàn)證
(2)在有圖片的時(shí)候加上對(duì)圖片的驗(yàn)證
<template> <div> <el-form ref="personform" label-position="right" label-width="120px" :model="formLabelAlign" status-icon :rules="rules"> <el-row> <el-col :span="12"> <el-form-item label="發(fā)布人"> <el-input size="mini" v-model="formLabelAlign.person" autocomplete="off" clearable :disabled="xiugai" ></el-input> </el-form-item> </el-col> </el-row> <el-row> <el-form-item label="簡(jiǎn)述"> <el-input type="textarea" v-model="formLabelAlign.paper" autocomplete="off"></el-input> </el-form-item> </el-row> <el-row> <el-form-item prop="file" ref="uploadpic"> <el-upload
style="display:inline-block" :limit="2" class="upload-demo" ref="upload" action="/hqx/knowledge/importKnowledge" :before-upload="beforeAvatarUpload" :auto-upload="false" :on-change="imageChange" :on-remove="imageRemove" :on-success="onsuccess" > <el-button slot="trigger" size="small" type="primary" plain>上傳</el-button> </el-upload> </el-form-item> </el-row> </el-form> </div> </template> <script> import _ from "lodash"; export default { data() { return { xiugai: false, formLabelAlign: { paper: "" }, images: [], rules: { file: [{ required: true, message: "請(qǐng)上傳圖片", trigger: "change" }] }, haspic: false // 默認(rèn)沒(méi)有傳圖片 }; }, methods: { beforeAvatarUpload(file) { // 文件類型進(jìn)行判斷 const isJPG = /^image\/(jpeg|png|jpg)$/.test(file.type); const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG) { this.$message.error("上傳圖片只能是 image/jpeg/png 格式!"); } if (!isLt2M) { this.$message.error("上傳圖片大小不能超過(guò) 2MB!"); } return isJPG && isLt2M; }, imageChange(file, fileList, name) { this.images["file"] = fileList; this.haspic = true; // 如果上傳了就不顯示提示 if (typeof this.images.file != "undefined") { if (this.images.file.length > 0) { this.$refs["uploadpic"].clearValidate(); } } }, imageRemove(file, fileList, name) { if (typeof this.images.file != "undefined") { if (this.images.file.length > 0) { this.$refs["uploadpic"].clearValidate(); } else { this.$refs["uploadpic"].validate(); this.haspic = false; } } }, onsuccess(response, file, fileList){ // 如果提交失敗將haspic改為false后邊的數(shù)據(jù)就不讓他提交 }, confirm() { if (this.haspic) { // 去掉rules中對(duì)圖片的驗(yàn)證 _.unset(this.rules, ["file"]); this.$refs["personform"].validate(valid => { if (valid) { console.log("說(shuō)明已經(jīng)添加了圖片直接提交就行了"); const wfForm = new FormData(); wfForm.append( 'dsc',this.formLabelAlign.paper) //直接將wfForm提交就可以 } else { console.log("error submit!!"); return false; } }); } else { alert('請(qǐng)?zhí)砑訄D片之后在提交') } } } }; </script>
藍(lán)藍(lán)設(shè)計(jì)建立了UI設(shè)計(jì)分享群,每天會(huì)分享國(guó)內(nèi)外的一些優(yōu)秀設(shè)計(jì),如果有興趣的話,可以進(jìn)入一起成長(zhǎng)學(xué)習(xí),請(qǐng)掃碼藍(lán)小助,報(bào)下信息,藍(lán)小助會(huì)請(qǐng)您入群。歡迎您加入噢~~希望得到建議咨詢、商務(wù)合作,也請(qǐng)與我們聯(lián)系。
文章來(lái)源:csdn
分享此文一切功德,皆悉回向給文章原作者及眾讀者.
免責(zé)聲明:藍(lán)藍(lán)設(shè)計(jì)尊重原作者,文章的版權(quán)歸原作者。如涉及版權(quán)問(wèn)題,請(qǐng)及時(shí)與我們?nèi)〉寐?lián)系,我們立即更正或刪除。藍(lán)藍(lán)設(shè)計(jì)( www.yvirxh.cn )是一家專注而深入的界面設(shè)計(jì)公司,為期望卓越的國(guó)內(nèi)外企業(yè)提供卓越的UI界面設(shè)計(jì)、BS界面設(shè)計(jì) 、 cs界面設(shè)計(jì) 、 ipad界面設(shè)計(jì) 、 包裝設(shè)計(jì) 、 圖標(biāo)定制 、 用戶體驗(yàn) 、交互設(shè)計(jì)、 網(wǎng)站建設(shè) 、平面設(shè)計(jì)服務(wù)
藍(lán)藍(lán)設(shè)計(jì)的小編 http://www.yvirxh.cn