Vee-validate: How to validate the same name field?

Created on 3 Jan 2018  路  4Comments  路  Source: logaretm/vee-validate

Versions:

  • VueJs: 2.2.1
  • Vee-Validate: 2.0.0

Description: I set some inputs that have same name, like name="name",but when one of those inputs unvalid,all of the inputs display the error,I want to know how to resolve this?

Steps To Reproduce:

my code:
1514974590 1

result:
3

duplicate

Most helpful comment

Adding key or index works to make vee-validate validate the fields correctly.

However, how to handle localizations?
In my case I suffix with -${index}, but this will print the following error messages:
"The field firstname-0 is required"
"The field firstname-1 is required"
etc...
Instead of "The field firstname is required" everywhere

Is there a solution for this?

EDIT SOLUTION

In v3.x the solution is to pass the _unique_ name to the :vid prop and pass the same name to the :name prop, e.g:

<tr v-for="(item, index) in items">
  <validation-provider :vid="`firstname-${index}`" name="firstname" rules="required|min:4">
    ...
  </validation-provider>
</tr>

More info in the v3.x docs

All 4 comments

vue provides a loop index, prefix this to the name property

Like @JessJNielsen said, the name is one of the unique identifiers for the errors. You need to make sure each field name is unique.

There are a lot of similar issues like #1051 and #996

Append the :key to the name and the error, making each value unique

```

Selection group name is required

Adding key or index works to make vee-validate validate the fields correctly.

However, how to handle localizations?
In my case I suffix with -${index}, but this will print the following error messages:
"The field firstname-0 is required"
"The field firstname-1 is required"
etc...
Instead of "The field firstname is required" everywhere

Is there a solution for this?

EDIT SOLUTION

In v3.x the solution is to pass the _unique_ name to the :vid prop and pass the same name to the :name prop, e.g:

<tr v-for="(item, index) in items">
  <validation-provider :vid="`firstname-${index}`" name="firstname" rules="required|min:4">
    ...
  </validation-provider>
</tr>

More info in the v3.x docs

Was this page helpful?
0 / 5 - 0 ratings