我有三个输入jobApplyEmail
,jobApplyPhone
,jobApplyOther
我正在使用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') }
}
},
我解决了我的问题
jobApplyEmail: {
requiredIf: requiredUnless(function() {
return (
this.form.jobApplyPhone !== '' || this.form.jobApplyOther !== ''
)
}),
email
},
jobApplyPhone: {
requiredIf: requiredUnless(function() {
return (
this.form.jobApplyOther !== '' || this.form.jobApplyEmail !== ''
)
})
},
jobApplyOther: {
requiredIf: requiredUnless(function() {
return (
this.form.jobApplyPhone !== '' || this.form.jobApplyEmail !== ''
)
})
}