elementUI 非表单格式的校验

这篇具有很好参考价值的文章主要介绍了elementUI 非表单格式的校验。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

elementUI 非表单格式的校验,elementui,vue.js,javascript

 在普通表单中对输入框、选择框都有校验案例。

但是在自定义非空中如何进行校验官网并没有说明

关键代码 clearValidate 方法清除校验

 this.$refs.formValue.clearValidate('signinimg')

使用案例文章来源地址https://www.toymoban.com/news/detail-560607.html

<template>
    <div class="stylebg">
      <Tabs icons="el-icon-shopping-bag-2" :arrs="tabs" />
     
     
      <!--表格  -->
      <div style="margin-top: 20px">
        <el-form ref="formValue"  label-width="120px" :rules="rules" :model="formValue">
            <el-form-item label="标题" prop="title">
              <el-input v-model="formValue.title" placeholder="请输入标题"></el-input>
            </el-form-item>
             
            
            <el-form-item label="单选图">
              <div class="imagelist">
                  <div class="imagelist imgbox" @click="uploadimg('img','1')" v-if="formValue.img">
                      <el-image class="img" :src="formValue.img" fit="cover" ></el-image>
                  </div>  
                  <div  style="border: 1px dashed #d9d9d9;" class="avatar-uploader"   @click="uploadimg('img','1')" v-else>
                      <i class="el-icon-plus" style="width: 100px;height: 100px;display: flex;align-items: center;justify-content: center;"></i>
                </div>
              </div>
            </el-form-item>
            <el-form-item label="多选图">
              <div class="imagelist">
                  <div v-for="(item, i) in formValue.imgs" :key="i" class="imagelist imgbox">
                      <el-image class="img" :src="item" fit="cover" ></el-image>
                      <img src="@/assets/close.png" class="close" @click="handleRemove(i)"/>
                  </div>  
                  <div  style="border: 1px dashed #d9d9d9;" class="avatar-uploader"   @click="uploadimg('imgs','2')">
                      <i class="el-icon-plus" style="width: 100px;height: 100px;display: flex;align-items: center;justify-content: center;"></i>
                </div>
              </div>
            </el-form-item>
            <el-form-item label="电子签名" prop="signinimg">
              <div  @click="signinVisible=true" style="width: 200px;background-color: #d9d9d9;">
                <el-image :src="formValue.signinimg" style="width: 200px;height: 100px;display: flex;align-items: center;justify-content: center;color: #999;">
                  <div slot="error" > 点击签名 </div>
                </el-image>
              </div>
            </el-form-item>
            <el-form-item label="">
              <el-button type="primary" size="small"  @click="upform">确定</el-button>
              <el-button type="primary" size="small"  @click.native="back">返回</el-button>
            </el-form-item>
        </el-form>
        
       
      </div>
      <el-dialog title="选择图片" :visible.sync="dialogVisible" width="900px">
        <uploadDialog  v-if="dialogVisible" @getPic="getPic" :chosetype="picChoseType"></uploadDialog>
      </el-dialog>

      <el-dialog title="电子签名" :visible.sync="signinVisible" width="700px">
       
          <signinDia @setsignin="setsignin"></signinDia>
        
      </el-dialog>


    </div>
  </template>
  
  <script>
 import wueditor from '@/components/wueditor.vue'; // ES6 Module
 import {addcaseAPI,getcaseinfoAPI,updatecaseAPI} from "@/api/article"
 import uploadDialog from "@/components/uploadDialog.vue"
 import signinDia from "@/components/signinDia.vue"
  export default {
    components: {
      wueditor,uploadDialog,signinDia
    },
    data() {
      return {
        formValue:{
          img:"",
          imgs:[],
          status:0
        },
       
        headers:{},
        tabs: ["轮播图管理", "添加轮播图"],
        dynamicTags: [],
        dialogVisible: false,
        signinVisible: false,
      
        inputValue: '',
       

        picTit:"",
        picChoseType:"1",
        rules:{
          title: [ { required: true, message: '名称不能为空', change: 'blur', }],
          signinimg: [ { required: true, message: '电子签名不能为空'  }],
        }
      };
    },
    created() {
      this.headers={
        Authorization:"Bearer " +localStorage.getItem('token')
      }
      this.getinfo()
      
    },
    methods: {
      upform(){
        this.$refs.formValue.validate((valid) => {
          if (valid) {
            alert("success")
          }else{
            if(this.formValue.signinimg) this.$refs.formValue.clearValidate('signinimg')
          }
        })
       
        // if (this.$route.query.id) {
        //   updatecaseAPI(this.formValue).then(res=>{
        //     this.$message.success("成功")
        //     this.$router.back()
        //   })    
        //   return
        // }
        // addcaseAPI(this.formValue).then(res=>{
        //   this.$message.success("成功")
        //   this.$router.back()
        // })    
         
      },
      setsignin(img){
        this.formValue.signinimg=img
        this.signinVisible=false
        this.$refs.formValue.clearValidate('signinimg')
      },
      uploadimg(picTit,picChoseType){
        this.dialogVisible=true
        this.picTit=picTit
        this.picChoseType=picChoseType
      },
      getPic(images){
        console.log(images)
        switch(this.picTit){
          case "img":
            this.formValue.img=images.url
          break
          case "imgs":
            var list=this.formValue.imgs

            this.formValue.imgs=list.concat(images.map(item=> item.url))
          break
        }
        this.dialogVisible=false
      },
      handleRemove(i) {
          this.formValue.imgs.splice(i,1) 
      },










      handleClose(tag) {
        this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      },

      showInput() {
        this.inputVisible = true;
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },

      handleInputConfirm() {
        let inputValue = this.inputValue;
        if (inputValue) {
          this.dynamicTags.push(inputValue);
        }
        this.inputVisible = false;
        this.inputValue = '';
      },
      getinfo(){
        var id=this.$route.query.id
        if (id) {
          getcaseinfoAPI({id}).then(res=>{
            this.formValue=res
            
          })
        }
        
      },
      handleAvatarSuccess2(res){
        this.formValue.img = res.data  
        console.log(res.data  )
      },
   
      back(){
        this.$router.back()
      },
    
  }
  };
  </script>
  
  <style  lang="less">
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  .form {
    margin: 15px 0;
  }
  .page {
    background-color: #fff;
    padding: 10px;
    .el-pagination .btn-next,
    .el-pagination .btn-prev {
      background-color: #ffffff !important;
    }
  }
  .imagelist{
  display: flex;
  align-items: center;
  .imgbox{    
    border: 1px solid #d9d9d9;
    position: relative;
    margin-right: 20px;
    .img{
      width: 100px;
      height: 100px;
      border-radius: 6px;
    }
    .close{
      width: 20px;
      height: 20px;
      position: absolute;
      top: -10px;
      right: -10px;
      cursor: pointer;
    }
  }
}
  </style>
  

到了这里,关于elementUI 非表单格式的校验的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包