vue iview表单验证,清除表单验证/去掉表单验证的*号

  • Post author:
  • Post category:vue


前言: 最近写isop项目遇到的问题,

表单验证,默认的iview写法加上prop,在data里面定义,input前面就会有个红色的

号,表示这里必填加了验证,测试觉得这个

号碍眼,让我去掉,第一次遇到这种需求,当时宕机了两秒,然后想着可以覆盖样式,确实可以
在这里插入图片描述

<template>
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80" class='detail_form'>
        <FormItem label="Name" prop="name">
            <Input v-model="formValidate.name" placeholder="Enter your name"></Input>
        </FormItem>
        <FormItem label="E-mail" prop="mail">
            <Input v-model="formValidate.mail" placeholder="Enter your e-mail"></Input>
        </FormItem>

        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')">Submit</Button>
            <Button @click="handleReset('formValidate')" style="margin-left: 8px">Reset</Button>
        </FormItem>
    </Form>
</template>
<script>
    export default {
        data () {
            return {
                formValidate: {
                    name: '',
                    mail: '',
                },
                ruleValidate: {
                    name: [
                        { required: true, message: 'The name cannot be empty', trigger: 'blur' }
                    ],
                    mail: [
                        { required: true, message: 'Mailbox cannot be empty', trigger: 'blur' },
                        { type: 'email', message: 'Incorrect email format', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            handleSubmit (name) {   // 提交时去验证
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.$Message.success('Success!');
                    } else {
                        this.$Message.error('Fail!');
                    }
                })
            },
            handleReset (name) {
                this.$refs[name].resetFields();   // 清除表单验证以及提示字段
            }
        }
    }
</script>

<style lang='less'> 
// 去掉*号样式
  .detail_form{
    .ivr-from-item-required .ivu-form-item-label:before {
      content: ' ';
      width: 0px;
      margin-right: 0px;
    }
  }
</style>



版权声明:本文为weixin_47239395原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。