Vee-validate: Fields of separate components are validated together if I inject validator manually

Created on 14 Oct 2018  ยท  5Comments  ยท  Source: logaretm/vee-validate

Versions

  • vee-validate: 2.1.0-beta.11 (also tested on 2.0.9)
  • vue: 2.5.17

Describe the bug
When I inject vee-validate in separate components, it validates all fields from all forms on the page together

To Reproduce
Steps to reproduce the behavior:

  1. Click any submit fields

Expected behavior
Separate validation for both components

Demo Link
https://codesandbox.io/s/j7yqy3m3p9
or
https://github.com/xorik/vee-validate-inject-bug-demo

Desktop (please complete the following information):

  • OS: Mac OS 10.14
  • Browser firefox
  • Version 62.0.3

Additional context
Without manual injection everything works perfectly: https://codesandbox.io/s/xvvv436384 (or https://github.com/xorik/vee-validate-inject-bug-demo/commit/b02fc9fb5e8fc2d964259bcc47626fb93a4f0ca9)

โ” question

Most helpful comment

I think you've confused the 2 uses of inject and $_veeValidate options. Your example uses 2 different forms. And uses inject: ['$validator'] so your intent wasn't clear, Each form would better get its own validation scope so you only need:

$_veeValidate: {
  validator: 'new' // give me my own validator scope.
}

The inject option will override the new validator and give you the parent instance, causing you to validate other fields.

Here is an updated example that uses the new validator instance given:

https://codesandbox.io/s/00wpv7ll7v

All 5 comments

Since both have the same validator, validateAll will trigger validation across all fields registered on that validator, this is not a bug. If you want to validate them individually then remove the inject option, or use scopes to group each form errors together.

https://codesandbox.io/s/k20qr4ypor

@logaretm thank you! That helped!

Just one question. In the documentation it said:

$_veeValidate: {
validator: 'new' // give me my own validator scope.
},

So I expect 2 independent scopes for these 2 forms. So I don't quite understand how it works

Actually it isn't so great. I can't use child component with different forms, because I have to hardcode scope now in the template in the errors.has() and errors.first() methods, so I can't reuse child components in different forms

I think you've confused the 2 uses of inject and $_veeValidate options. Your example uses 2 different forms. And uses inject: ['$validator'] so your intent wasn't clear, Each form would better get its own validation scope so you only need:

$_veeValidate: {
  validator: 'new' // give me my own validator scope.
}

The inject option will override the new validator and give you the parent instance, causing you to validate other fields.

Here is an updated example that uses the new validator instance given:

https://codesandbox.io/s/00wpv7ll7v

@logaretm oh, that's cool, thanks so much!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MaxMilton picture MaxMilton  ยท  3Comments

Shchepotin picture Shchepotin  ยท  3Comments

kidox picture kidox  ยท  3Comments

the94air picture the94air  ยท  3Comments

HunterJS-bit picture HunterJS-bit  ยท  3Comments