Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate Vuetify text field asynchronously?

Text fields in Vuetify have rules props, which take an array of functions returning true or an error string. How to make them async, so that the validation could be made server-side using XHR?

Something like:

<v-text-field :rules="[v => { axios.get('/check?value=' + val).then(() => { return true }) }]">
like image 993
niutech Avatar asked Mar 06 '18 13:03

niutech


1 Answers

One solution is to set the error-messages prop:

<v-text-field v-model="input" :error-messages="errors">

and use the watch option:

new Vue({
  data () {
    return {
      input: '',
      errors: []
    }
  },
  watch: {
    input (val) {
        axios.get('/check?value=' + val).then(valid => {
          this.errors = valid ? [] : ['async error']
        })
    }
  }
});
like image 141
niutech Avatar answered Nov 09 '22 11:11

niutech