除非需要-只需填写一个输入

时间:2020-06-17 20:30:56

标签: vue.js nuxt.js bootstrap-vue vuelidate

我有三个输入jobApplyEmailjobApplyPhonejobApplyOther,我正在用Vuelidate进行验证。在这三个输入中,要求用户至少输入一个输入。为此,我使用了requiredUnless,但是由于某种原因,它无法正常工作。

模板重复

// Using bootstrap-vue
<b-form-input
  id="jobApplyEmail"
  v-model="form.jobApplyEmail"
  type="email"
  :class="{ 'is-invalid': $v.form.jobApplyEmail.$error }"
  @blur="$v.form.jobApplyEmail.$touch()">
</b-form-input>

脚本

import {
  required,
  email,
  requiredUnless
} from 'vuelidate/lib/validators'
data() {
    return {
      form: {
        jobApplyEmail: '',
        jobApplyPhone: '',
        jobApplyOther: ''
      },
    }
  },
computed: {
    isOptional: () => {
      return (
        this.jobApplyEmail !== '' ||
        this.jobApplyOther !== '' ||
        this.jobApplyPhone !== ''
      )
    }
  },

validations: {
    form: {
      jobApplyEmail: { required: requiredUnless('isOptional'), email },
      jobApplyOther: { required: requiredUnless('isOptional') },
      jobApplyPhone: { required: requiredUnless('isOptional') }
    }
  },

1 个答案:

答案 0 :(得分:0)

我解决了我的问题

\w