Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize error message (field name) in VeeValidate?

When VeeValidate picks up an invalid field it outputs an error using the fields name, eg.

The address_line_1 field is required.

Is it possible to use a fields label or some other attribute in the error message, as field names are not always user friendly?

like image 493
panthro Avatar asked Mar 28 '17 12:03

panthro


People also ask

Why is my veevalidate rule not working on empty fields?

When the field under validation is not required, your rule may not be executed at all. This is because VeeValidate skips validation for empty fields if they are not required. You can override this behavior by using v-validate.continues modifier to force all rules to run.

How do I add custom rules to veevalidate?

You can easily add custom rules to VeeValidate, but your custom rules must adhere to a contract or certain structure: This is the most basic custom validator form. It consists of only a function that returns either a Boolean or a promise. However, it will have a default error message.

How to create a vee validate plugin?

Install the Vee Validate library: After installation, create an empty folder plugin in the src folder to set up the validator. In the folder created, add a new file: We are importing and registering the components from the vee-validate library, giving them aliases so as not to collide with HTML elements.

How to validate non require-like rules in veevalidate?

For validation providers the target field must have a vid prop set instead of the ref. If you need to define a rule that declares a field as required under specific conditions, you will need to enable the computesRequired option. Because, by default, VeeValidate doesn't validate non require-like rules if the field has no value.


2 Answers

For VeeValidate v3 you can pass name attribute for the ValidationProvider

<ValidationProvider name="first name" rules="required|min:2" v-slot="{ errors }">
like image 127
Mārtiņš Briedis Avatar answered Oct 16 '22 10:10

Mārtiņš Briedis


You can use data-vv-as which will show in the error message. Read more here.

<input type="text" name="address_line_1" data-vv-as="Address Line 1" />

EDIT: Updated documentation reference here.

like image 23
thefallen Avatar answered Oct 16 '22 09:10

thefallen