el-form rules 验证失败可能原因

钱欣然
2023-12-01
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="活动名称" prop="name">
    <el-input v-model="ruleForm.name"></el-input>
  </el-form-item>
  </el-form>
  
 export default {
    data() {
      return {
        ruleForm: {
          name: '',
        },
        rules: {
          name: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          }
     },
     mehtods:{
	submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
	}
}


失败原因,
1,你的dom是弹出框子组件等。form没有找到,使用this.$refs.formName.validate试试
2,<el-form-item label="活动名称" prop="name"> 没有加prop="name",这prop必须加
3,![这几个字段必须有](https://img-blog.csdnimg.cn/20201121220323868.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzk3OTUwMw==,size_16,color_FFFFFF,t_70#pic_center)

 类似资料: