How to redefine a Vee Validate error message for one Vue component?

Viewed 2398

I have a global validation rule, for example:

import { extend } from 'vee-validate';
import { required } from 'vee-validate/dist/rules';

extend('required', {
  ...required,
  message: 'Please fill the field'
});

This rule is using for all Vue components across the project. But for one exact component I need to redefine the message Please fill the field to another one. Is it possible to change the message just for one Vue component?

1 Answers

You can specifiy specific messages for each ValidationProvider component using the custom-messages prop

<ValidationProvider rules="required" :custom-messages="{ required: 'required message' }">
  <!-- ... -->
</ValidationProvider>

You can extract it on a data prop and use it for the providers in your component:

<template>
  <ValidationProvider rules="required" :custom-messages="customMessages">
    <!-- ... -->
  </ValidationProvider>

  <ValidationProvider rules="required" :custom-messages="customMessages">
    <!-- ... -->
  </ValidationProvider>
</template>

<script>
export default {
 // ....
 data: () => ({
    customMessages: {
      required: 'custom message'
    }
  }),
  // ...
};
</script>
Related