8

I am using Laravel - 5.8 with Vue.js. My question is about how to show a custom error message for a rule in the Vee-Validate library. My custom message for the "required" rule is not showing, and instead it reads: "The first_name field is required." The expected message is "Please enter first name."

Below code is in app.js

import { ValidationProvider } from 'vee-validate/dist/vee-validate.full';

This is my HTML component code.

<template>    
    <div>
        <form role="form">
            <ValidationProvider name="first_name" :rules="required">
                <div slot-scope="{ errors }">
                    <input v-model="profileForm.first_name" class="form-control">
                    <p>{{ errors[0] }}</p>
                </div>
            </ValidationProvider>
                  
            <button type="button" @click="validateBeforeSubmit()">Update Profile</button>
        </form>
    </div>
</template>

Below is my JS script code

<script>
    import { localize } from 'vee-validate/dist/vee-validate.full';
    import en from "vee-validate/dist/locale/en.json";

    export default {
        data() {
            return {
                profileForm: {
                    first_name: ''
                },
                customMessages: {
                    en: {
                        custom: {
                            'first_name': {
                                required: 'Please enter first name'
                            }
                        }
                    }
                },
            }
        },
        created() {
            localize("en", this.customMessages);
        },
        methods: {
            validateBeforeSubmit() {
                this.$validator.validateAll();
            }
        }
    }
</script>

Am I missing anything?

zcoop98
  • 2,590
  • 1
  • 18
  • 31
Pankaj
  • 9,749
  • 32
  • 139
  • 283

7 Answers7

8

One way to do this generically, without attaching it to a specific language like with localize, is to use extend():

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

extend('required', {
    ...required,
    message: 'Please enter first name',
});

This will spread and include all the default properties of the rule, while still allowing you to add in your own custom message.

i18n Example:

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

extend('required', {
    ...required,
    message: i18n.t('LOCALIZATION_PATH'),
});
zcoop98
  • 2,590
  • 1
  • 18
  • 31
5

custom keyword has been removed in version 3. It is now replaced with fields. Also this info was missing in docs

For more info please follow this issue link on github

Pankaj
  • 9,749
  • 32
  • 139
  • 283
2

Very simple one. Although not good, but works.

<input name="yourFieldName" type="text" class="form-control" v-model="yourFieldName" v-validate="'alpha_spaces'">

<span class="small text-danger">{{ errors.first('yourFieldName')?'Your custome message':'' }}</span>
Daud khan
  • 2,413
  • 2
  • 14
  • 18
1

Thanks to @pankaj for the right answer. For those who cant be bothered to follow the links, the documentation indicates the following solution:

import { localize } from 'vee-validate';

localize({
  en: {
    messages: {
      // generic rule messages...
    },
    fields: {
      password: {
        required: 'Password cannot be empty!',
        max: 'Are you really going to remember that?',
        min: 'Too few, you want to get doxed?'
      }
    }
  }
});
omarjebari
  • 4,861
  • 3
  • 34
  • 32
1

With vee-validate 3

<ValidationProvider
  name="first_name"
  rules="required"
  :custom-messages="{required: 'Ooops, this is the message'}"
>
Sergey Onishchenko
  • 6,943
  • 4
  • 44
  • 51
0

I think you need to pass this.customMessages.en to localize() or the passed object has a top-level property en. The passed dictionary should only contain your custom messages.

Olivenbaum
  • 971
  • 1
  • 6
  • 17
  • I have now this code: `localize("en", this.customMessages.en);`. This is still not showing me my custom messages – Pankaj Sep 18 '19 at 02:37
0

Custom messages can be added via 'extend' as @ zcoop98 showed. It should be noted that the property 'will be translated if we use it like this message: () => i18n.t ('LOCALIZATION_PATH')

Janusz O
  • 1
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/29811904) – ryanttb Sep 13 '21 at 22:19