2017-07-18 84 views
0

我試圖更改由使用momement.js庫的函數的vee-validate生成的錯誤消息。我可以更改其他驗證規則的消息,例如'required',但不能'date_format','before'或'after'。這是因爲這些規則使用了moment.js庫嗎?有什麼方法可以更改這些錯誤消息嗎?更改'date_format','之後'或'之前'的vee驗證錯誤消息

Vue.use(VeeValidate,{ 
    fieldsBagName: 'vFields', 
    dictionary: { 
    en: { 
     messages: { 
     required: 'Your field is empty', // messages can be strings as well. 
     date_format: 'wrong format for date', 
     after:"date is too old", 
     } 
    } 
    } 
}); 

new Vue({ 
    methods: { 
    validate() { 
     this.$validator.validateAll().then(() => { 
     alert('Is valid') 
     }).catch(() => { 
     alert('Is not valid') 
     }) 
    } 
    } 
}).$mount('#app') 

See JSFiddle

回答

0

我一直在使用我的組件內部下面的代碼來實現這一點:

import VeeValidate from 'vee-validate' 
const dictionary = { 
    en: { 
     messages: { 
      after: (field, args) => `The ${field} must be greater than ${moment(args, moment.defaultFormat).format('YYYY-MM-DD HH:mm')}.`, 
      date_format: (field, args) => `The ${field} is invalid.` 
     } 
    } 
} 

VeeValidate.Validator.updateDictionary(dictionary);