Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue2 plus vee-validate - how to customize error messages without ES6?

Tags:

vue.js

vuejs2

I am trying (without a success) to customize error messages for vee-validate, but all the examples on the website use ES6. I can bet it's doable also without it, so any suggestions what I am doing wrong are appreciated :)

<script>
    const messages = {
      en: {
        confirmed: "Your password is not confirmed",
        email: "I really dont like your email"
      }
    };

    Vue.use(VeeValidate);
    var app = new Vue({
        el: '#app'            
    });
    app.$validator.updateDictionary(messages);
</script>

There are no errors, simply the default messages are used.

UPDATE

Below is my HTML code.

<input type="text" name="email" v-validate data-vv-rules="required|email" />
<span v-show="errors.has('email')">{{ errors.first('email') }}</span>

<input type="password" name="password" v-validate data-vv-rules="required" />
<span v-show="errors.has('password')">{{ errors.first('confirmation')}} </span>  
<input type="password" name="confirmation" v-validate data-vv-rules="confirmed:password"/>
<span v-show="errors.has('confirmation')">{{ errors.first('confirmation')}}/span>
like image 259
Lech Migdal Avatar asked Dec 10 '16 07:12

Lech Migdal


1 Answers

() => is syntax to define function in ES6, so to convert syntax of vee-validate to older Javascript.

so from the documentation:

alpha: () => 'Some English Message'

will be equivalent of

alpha: function() {
  return 'Some English Message'
}

Similarly, you have to make following changes:

<script>
    const messages = {
      en: {
        confirmed: function () { 
            return "Your password is not confirmed"
        },
        email: function () { 
            return "I really dont like your email"
        }
      }
    };

    Vue.use(VeeValidate);
    var app = new Vue({
        el: '#app'            
    });
    app.$validator.updateDictionary(messages);
</script>

Updated Answer with working example

There seems to be some syntax errors in the above code, Following is the working code with latest vee-validate syntax:

<script>
const dictionary = {
  en: {
    messages: {
      confirmed: function () { 
        return "Your password is not confirmed"
      },
      email: function () { 
        return "I really dont like your email"
      }
    }
  }
};

VeeValidate.Validator.updateDictionary(dictionary);
Vue.use(VeeValidate);
Vue.config.debug = true;
var App = new Vue({
    el: '#app'
});
</script>

Working fiddle.

like image 107
Saurabh Avatar answered Oct 25 '22 16:10

Saurabh