vue.jsvuejs2vuelidate

Vuelidate make input field 1 or input field 2 required "Or" validator?


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
    },
 }
}

Solution

  • 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)
          }},
       },