0

I’m trying to validate a 6 digit code with vuelidate. If this.validationcode equals false I want to show a validation error. I'm pretty new to vue so I'm not entirely sure where I'm going wrong. How do I get this to work?

The error I get is:

TypeError: Cannot read property '__isVuelidateAsyncVm' of undefined

JS

data() {
    return {
      validationcode: false,
      validationRules: [
        { vcode: { 
            required,
            numeric,
            minLength: minLength(6),
            maxLength: maxLength(6),
            validCode () { return this.validationcode }
          } },
      ],
    };
  },

I also tried it as an arrow function but it doesn't get the value properly from the looks of it.

validCode: () => {
 console.log("the val code is " + this.validationcode)
 return this.validationcode 
}

HTML - v.formData.vcode.validCode - In the current front end view, this rule is triggered every time.

<div class=“form-group”>
        <input
          type=“text”
          class=“form-control”
          :class=“hasError(‘vcode’) ? ‘is-invalid’ : ‘’”
          placeholder=“Enter your 6 digit code”
          v-model=“formData.vcode”
        />
        <div v-if=“hasError(‘vcode’)” class=“invalid-feedback”>
          <div class=“error” v-if=“!$v.formData.vcode.required || !$v.formData.vcode.minLength || !$v.formData.vcode.maxLength”>
            Enter your 6 digit code
          </div>
          <div class=“error” v-if=“!$v.formData.vcode.numeric”>
            Should be a valid value.
          </div>
          <div class=“error” v-if=“!$v.formData.vcode.validCode”>
            Code incorrect, please try again.
          </div>
        </div>
</div>

This is the method that I am assigning true/false to this.validationcode.

verifyNumber() {


      var numbers = /^[0-9]+$/;
      if (code.match(numbers)) {
        // Twilio functions do not accept multipart/form-data
        const data = new URLSearchParams();
        data.append("phone_number", m.mobileNumber);
        data.append("verification_code", code);

        fetch("https://saffron-cheetah-1234.twil.io/check", {
          method: "POST",
          body: data,
        })
          .then((response) => response.json())
          .then((json) => {
            if (json.success) {

              console.log("Successfully verified the token.");
              this.validationcode = true;

            } else {

              this.validationcode = false;
              console.log(this.validationcode);
              console.log("Incorrect token.");
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
      }
    },
deeej
  • 357
  • 1
  • 6
  • 22
  • I think that you should use an arrow function for your validator - otherwise it probably does not have access to `this` – IVO GELOV Oct 19 '20 at 06:37
  • Thanks Ivo. But I tried this and I get the same issue. validCode: () => { console.log("the val code is " + this.validationcode) return this.validationcode } – deeej Oct 19 '20 at 06:43
  • well, you should be able to put a breakpoint inside the validator and see what actually happens – IVO GELOV Oct 19 '20 at 15:26
  • I can see that the issue is with getting the value of this.validationcode. https://github.com/vuelidate/vuelidate/issues/176 This seems to kind of be related, however even when I don't use an arrow function as suggested I still have the same issue. – deeej Oct 19 '20 at 23:57
  • Well, if an arrow function does not help - you can try putting your function inside the component's `methods` section. – IVO GELOV Oct 21 '20 at 07:36
  • Thanks @IVOGELOV I tried that but still the same issue. I added this to my methods section validCode: function () { if(this.validationcode === 1 ){return true} }, and this to my validation rule validCode: this.validCode but I still get vue.runtime.esm.js?2b0e:1888 TypeError: Cannot read property '__isVuelidateAsyncVm' of undefined – deeej Oct 21 '20 at 08:01
  • while using vuelidate for validations try this, ```validCode: function() { return this.validationcode }```. – lokanath Mar 16 '21 at 03:13

0 Answers0