Vee-validate: How to validate only some fields

Created on 2 Jan 2018  ยท  1Comment  ยท  Source: logaretm/vee-validate

Versions:

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

Description:

I have a form with four fields and two buttons. All of the fields have validation rules on them.

I want to validate only two fields if button 1 is clicked, and only the other two if button 2 is clicked.

I'm envisioning it like this:

buttonOneSubmit() {
    this.$validator.validateSome(["Field1", "Field2"]).then(result => {
        if (!result) {
            return false
        }
        //etc.
    }).catch((error) => {
    })
},   

Obviously validateSome is not an actual function, but I'm looking for something where I can feed it an array of field names and it validates only those.

Is this possible? I looked through the documentation but found the examples to be bewilderingly confusing.

โ” question

Most helpful comment

Sorry about the current state of the documentation.

Anyways, you cannot validate a specific set of fields, but you can either scope them like mentioned in the scope example which can be thought as groups for fields.

Another solution is to validate the fields manually and collect the results using Promise.all:

const results = Promise.all([
  this.$validator.validate('field1'),
  this.$validator.validate('field2')
]);

const areValid = (await results).every(isValid => isValid);

You can refactor that code into a function that takes an array of name fields.

>All comments

Sorry about the current state of the documentation.

Anyways, you cannot validate a specific set of fields, but you can either scope them like mentioned in the scope example which can be thought as groups for fields.

Another solution is to validate the fields manually and collect the results using Promise.all:

const results = Promise.all([
  this.$validator.validate('field1'),
  this.$validator.validate('field2')
]);

const areValid = (await results).every(isValid => isValid);

You can refactor that code into a function that takes an array of name fields.

Was this page helpful?
0 / 5 - 0 ratings