How to localize error messages in vuejs using vee-validation

早过忘川 提交于 2019-12-08 03:46:26

In your code example, this.$validator is called in the middle of nowhere... you need to put it inside your Vue instance, in the mounted hook for example :

new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: {
    App
  },
  mounted() {
    this.$validator.localize('en', {
      messages: {
        required: (field) => '* ' + field + ' is required'
      },
      attributes: {
        email: 'Email'
      } 
    })
  }
})

If we want to Change all Default Message include some attributes changes. Define you message and then mounted in main js file

         const dict = {
            messages: {
                required: (field) => 'Please Enter ' + field + ''
            },
            attributes: {
                name: 'Name',
                email: 'Email Id '
            }
        }
        const app = new Vue({
            el: '#app',
            router: router,
            components: {
                App
            },
            mounted() {
                this.$validator.localize('en', dict);
            }
        });

  //  Make to change in single required message
    const dict = {
        custom: {
            email: {
                required: 'Please Enter Valid Email Id'
            },
            name: {
                required: 'Name include first and last name'
            }
        }
    }
    const app = new Vue({
        el: '#app',
        router: router,
        components: {
            App
        },
        mounted() {
            this.$validator.localize('en', dict);
        }
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!