Vee-validate: Question: Custom messages per rule and field

Created on 17 May 2018  ยท  5Comments  ยท  Source: logaretm/vee-validate

Versions:

  • VueJs: 2.5.16
  • Vee-Validate: 2.0.9

Assumptions

  • Vue SSR
  • I have an input component
  • Scoped form

Question:

How would I do the following?
I'm trying to make the required message for firstName be:

Please enter your first name.

and for city:

Please enter the city.

Steps To Reproduce:

So far I have:

const dictionary = {
  en: {
    messages: {
      required: (field) => {
        if (field === 'firstName') {
          return `Please enter your ${field}.`
        } else if (field === 'city') {
          return `Please enter the ${field}.`
        }
        return `Please enter a ${field}.`
      },
    },
  },
}

Object.keys(veeRules).forEach(k => Validator.extend(k, veeRules[k]))
Validator.localize('en', veeEn)
Vue.use(VeeValidate, { inject: false, dictionary })

But this doesn't seem correct.

โ” question

Most helpful comment

You could assign specific error messages to specific fields:

const dictionary = {
  en: {
    custom: {
      firstName: {
         required: 'Please enter your first name.'
      },
      city: {
        required: 'Please enter the city.'
      }
    }
  }
}

Object.keys(veeRules).forEach(k => Validator.extend(k, veeRules[k]))
Validator.localize('en', veeEn)
Vue.use(VeeValidate, { inject: false, dictionary })

All 5 comments

You could assign specific error messages to specific fields:

const dictionary = {
  en: {
    custom: {
      firstName: {
         required: 'Please enter your first name.'
      },
      city: {
        required: 'Please enter the city.'
      }
    }
  }
}

Object.keys(veeRules).forEach(k => Validator.extend(k, veeRules[k]))
Validator.localize('en', veeEn)
Vue.use(VeeValidate, { inject: false, dictionary })

Hi @logaretm I'm trying this out but I keep getting: "Can't find variable: veeRules". I'm obviously doing something wrong. Please can you give me pointers?

Never mind--I fixed this myself now.

@elujoba I know that you fixed it, but more info is here: https://github.com/baianat/vee-validate/issues/1329

@logaretm Thanks a million!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MeltedFreddo picture MeltedFreddo  ยท  3Comments

MaxMilton picture MaxMilton  ยท  3Comments

yyyuuu777 picture yyyuuu777  ยท  3Comments

HunterJS-bit picture HunterJS-bit  ยท  3Comments

the94air picture the94air  ยท  3Comments