Vee-validate: this.error.add -> add message from dictionary

Created on 9 Nov 2017  ยท  1Comment  ยท  Source: logaretm/vee-validate

Versions:

  • VueJs: 2.5.2
  • Vee-Validate: 2.0.0-rc.21

Description:

I have a simple login form and if the user enters an incorrect password or username the error message gets displayed

<form action="POST" >
    <span v-show="errors.has('credentials')" class="help is-danger">{{ errors.first('credentials') }}</span>
    <p class="form-group">
    ...
onSignin() {
    axios.post(
        'api/user/signin',
        {
            email: this.email,
            password: this.password,
        },
        {
            headers: { 'X-Requested-With': 'XMLHttpRequest' },
        },
    ).then((response) => {
            this.$router.push('dashboard');
        }).catch((error) => {
            this.errors.add('credentials', 'Wrong user or Password'); //this message i want to move to the dictionary
        });
    },

This works fine, but how can i add this message to the dictionary with localize that i have all my messages in one place?

โ” question

Most helpful comment

You can add the credentials as a custom rule for a computed field and let the validator handle adding the messages itself.

Or if you want to keep adding them manually you can first add the message template to the dictionary:

import { Validator } from 'vee-validate';

Validator.dictionary.merge({
  en: {
    messages: {
      credentials: 'Wrong user or password'
    }
  }
});

Then you need to fetch the message when adding it from the dictionary:

const message = this.$validator.dictionary.getMessage('en', 'credentials');
this.errors.add('credentials', message); //this message i want to move to the dictionary

>All comments

You can add the credentials as a custom rule for a computed field and let the validator handle adding the messages itself.

Or if you want to keep adding them manually you can first add the message template to the dictionary:

import { Validator } from 'vee-validate';

Validator.dictionary.merge({
  en: {
    messages: {
      credentials: 'Wrong user or password'
    }
  }
});

Then you need to fetch the message when adding it from the dictionary:

const message = this.$validator.dictionary.getMessage('en', 'credentials');
this.errors.add('credentials', message); //this message i want to move to the dictionary
Was this page helpful?
0 / 5 - 0 ratings

Related issues

parweb picture parweb  ยท  3Comments

jagasan picture jagasan  ยท  3Comments

7immer picture 7immer  ยท  3Comments

biapar picture biapar  ยท  3Comments

MeltedFreddo picture MeltedFreddo  ยท  3Comments