【问题标题】:How to make Vue's element-ui validateField() works with v-if?如何让 Vue 的 element-ui validateField() 与 v-if 一起工作?
【发布时间】:2018-03-27 18:41:55
【问题描述】:

请查看密码字段。 PasswordConfirm Password 字段在单击 Change Password? 按钮时显示。

以下代码可以正常工作,并按预期使用v-show 验证表单,但在使用v-if 时不验证。

我了解v-showv-if 的作用,而data(){} 中的功能在element-ui 的文档中是这样的。这是文档的网址:http://element.eleme.io/#/en-US/component/form#custom-validation-rules

<template lang="pug">
  el-dialog( width="600px", title="Users", :visible.sync="dialogVisible")
    el-form.demo-ruleForm(:model="editedItem", status-icon, :rules="formRules", ref="userForm", label-width="140px")
      el-form-item(label="Name", prop="firstName")
        el-input(v-model="editedItem.name", auto-complete="off")

      template(v-if="!changePassword")
        el-form-item
          el-button(@click="changePassword = true") Change Password?
      template(v-else)
        el-form-item(label="Password", prop="password")
          el-input(type="password", v-model="editedItem.password", auto-complete="off")

        el-form-item(label="Confirm Password", prop="confirmPassword")
          el-input(type="password", v-model="editedItem.confirmPassword", auto-complete="off")

    .dialog-footer(slot="footer")
      el-button(type="primary", @click="submitForm('userForm')") Save
</template>

<script>
export default {
  name: 'dialog-add-edit-user',
  props: {
    editedItem: Object,

  },
  data () {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('Please input the password'))
      } else {
        if (this.confirmPassword !== '') {
          this.$refs.userForm.validateField('confirmPassword')
        }
        callback()
      }
    }

    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('Please input the password again'))
      } else if (value !== this.editedItem.password) {
        callback(new Error('Two inputs don\'t match!'))
      } else {
        callback()
      }
    }

    return {
      formRules: {
        password: [
          {
            validator: validatePass,
            trigger: 'blur'
          }
        ],
        confirmPassword: [
          {
            validator: validatePass2,
            trigger: 'blur'
          }
        ]
      },
      dialogVisible: false,
      changePassword: false,
      editedItem: {
        name: '',
        password: '',
        confirmPassword: ''
      }
    }
  },

  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('save-item')
          console.log('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

【问题讨论】:

  • 似乎工作正常,jsfiddle.net/fLw3ek9v 你能澄清什么不工作吗?
  • @Daniel,感谢您的帮助。我不确定它是如何为你工作的。另外,我在你的 jsfiddle 中注意到你已经放置了 const validatePassconst validatePass2 两次,这与代码工作有关吗?而且,验证消息仅显示Confirm Password
  • 它们不需要出现两次,我只是将它们移出 vue 实例而忘记删除。

标签: javascript vue.js vuejs2 element-ui


【解决方案1】:

好的,我想我明白问题所在了。

您的验证通过了,但它不检查namepassword,只检查passwordChange

因此,如果您“...了解 v-show 和 v-if 的作用”,您就会知道使用 v-if/v-else 时这些元素不存在。它们会根据需要添加和删除。

这是一个问题的原因是元素库在添加时经历了初始化阶段。稍后使用$ref 引用该元素。看看SubmitForm,它使用了`this.$refs[formName].validate'

所以当你使用v-if/v-else 时,由于元素一开始就不存在,它们将不会被正确调用。

您有两个选择,要么坚持使用v-show,这应该很容易,或者您可以使用我一直在利用的不允许强制手动或自动重新加载的 3rd 方库的 hack。 hack 包括在主要元素中添加一个key。所以 html 看起来像这样。

<el-form
  class="demo-ruleForm"
  :model="editedItem"
  status-icon="status-icon"
  :key="'form'+changePassword"  <--- this is the magic
  :rules="formRules"
  ref="userForm"
  label-width="140px"
>

在哈巴狗中

el-form.demo-ruleForm(:model="editedItem", :key="'form'+changePassword", status-icon, :rules="formRules", ref="userForm", label-width="140px")

【讨论】:

  • 并且名称不起作用,因为您将道具定义为 prop="firstName" 但实际上在其他任何地方都是 name
猜你喜欢
  • 2019-11-17
  • 1970-01-01
  • 2017-02-27
  • 2022-08-16
  • 2020-08-19
  • 2019-03-23
  • 1970-01-01
  • 2019-02-07
  • 2023-02-02
相关资源
最近更新 更多