0

I am using vuelidate, and it is working perfect. But I want to do a request in a custom validator. See the code:

validations: {
  partnumberValue: {
    required,
    isValid(value) {
      if (value.length < 12 || value.length > 12) return false;

      return new Promise((resolve, reject) => {
        vm.http.get('/api/test/' + value).then((response) => {
          resolve(response.json())
        }, (response) => {
          reject(response.status)
        })
      });
    }
  }
}

But I get the following error:

TypeError: Cannot read property 'get' of undefined

So I thought that I don't have acces to the vue instance? I have tried to bind the vue instance to the window object, like this:

Vue.use(VueResource);
Vue.use(BootstrapVue);
Vue.use(Vuelidate);

const vm = new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: {
    App
  }
});

window.vm = vm;

I have also tried to save the this instance globally in the file itself. But that isnt working. How can I do a http request inside a vue validator?

The library: Vuelidator

Rick
  • 4,030
  • 9
  • 24
  • 35
da1lbi3
  • 4,369
  • 6
  • 31
  • 65

0 Answers0