英文:
Vuelidate make input field 1 or input field 2 required "Or" validator?
问题
我正在尝试将 Vuelidate 集成到我的 Vue 表单中。我有两个字段,电话号码和电子邮件。我想检查它们中的任何一个是否已设置。我假设这是“或”验证器,但我找不到任何如何实现它的示例。我在这里找到了一些东西,尝试复制它,但得到了"phone not defined"。我也在 GitHub 上找到了一些东西,但这也不起作用。我只是不明白这应该如何工作。因此,验证应该通过如果其中一个输入,只有在两个字段都为空时才会失败。我理解"Or"验证器的正确方式吗?如果是的话,它应该如何工作?希望有人能给我一些提示。非常感谢您的提前帮助。
英文:
I am trying to implement Vuelidate into my Vue Form. I have 2 fields, Phone Number and Email. I want to check if either of them is set. I assume this is the "Or" Validator, but I can not find any examples how to implement it.I found something on here and tried to copy it, but get
phone not defined. I also found something on GitHub, but again this was not working. I justy dont understand how this is supposed to work. So Validation should pass if either is entered and only fail if both fields are empty. Am I understanding the Or Validator correct and if so, how is it supposed to work? I hope someone can give me a hint. Thank you very much in advance.
data: () => ({
form: {
email: '',
phone:'',
},
}),
validations: {
form: {
email:{
required,
mycustomval: or(phone)
},
phone:{
required
},
}
}
答案1
得分: 0
我现在已经让它正常工作了。问题是我使用了 form:{},所以我需要使用 this.form.phone 或 this.form.mobile 来获取值。
export default {
mixins: [validationMixin],
data: () => ({
form: {
email: '',
phone: '',
},
}),
validations: {
form: {
email: {
required(v) {
return this.form.phone || required(v);
},
},
phone: {
required(v) {
return this.form.email || required(v);
},
},
},
},
}
希望这对你有帮助。
英文:
I now got it to work. The problem was that I used form:{} so I need to use this.form.phone or this.form.mobile to get the value.
export default {
mixins: [validationMixin],
data: () => ({
form: {
email: '',
phone:'',
},
}),
So to get it to work I have to use:
validations: {
form: {
email:{
required (v) {
return this.form.phone|| required(v)
}},
phone: {
required (v) {
return this.form.email|| required(v)
}},
},
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论