Vee-validate: Pass an object to custom validation

Created on 12 May 2017  ·  5Comments  ·  Source: logaretm/vee-validate

Versions:

  • VueJs: #.#.#
  • Vee-Validate:

Description:

Hello logaretm, So firstly i want to thank you for you incredible repository, I'm using vuetify with vee-validate, I confess that you have the best vuejs validation library in all the web, It is complete with an incredible documentation.
I have a question :
There is a way to passe an object to custom validation, because I create my inputs fields with a loop and each input contains an object

For example

<p v-for="inventory in List">
<v-text-field
    :label="'Stock ('+inventory.currentStock+' '+inventory.type+')'"
    :name="inventory.nameStock"
    v-model="inventory.stock"
    v-bind:rules="inventory.rulesStock"
    v-validate="'required|custom_valid'"
></v-text-field>
</p>

There is for example a directive like : v-pass-args-to-custom-validation="inventory"?
Or something that can help me in my case?
Thanks so much in advance.

Steps To Reproduce:

❔ question

Most helpful comment

Thank you for the kind words 😄 . You can validate objects, if your input knows how to deal with them. for example I can't see how the text field component would be able to deal with an object, eventually you are validating the object stock value which is a string/number.

anyways, you can create a custom rule that accepts said objects as params:

const myRule = (obj) => {
    // validate stuff
};

and in your html you can use the object form:

<v-text-field
    :label="'Stock ('+inventory.currentStock+' '+inventory.type+')'"
    :name="inventory.nameStock"
    v-model="inventory.stock"
    v-bind:rules="inventory.rulesStock"
    v-validate="{
        required: true,
        custom_rule: [inventory]
    }"
></v-text-field>

All 5 comments

Thank you for the kind words 😄 . You can validate objects, if your input knows how to deal with them. for example I can't see how the text field component would be able to deal with an object, eventually you are validating the object stock value which is a string/number.

anyways, you can create a custom rule that accepts said objects as params:

const myRule = (obj) => {
    // validate stuff
};

and in your html you can use the object form:

<v-text-field
    :label="'Stock ('+inventory.currentStock+' '+inventory.type+')'"
    :name="inventory.nameStock"
    v-model="inventory.stock"
    v-bind:rules="inventory.rulesStock"
    v-validate="{
        required: true,
        custom_rule: [inventory]
    }"
></v-text-field>

Hello @logaretm That's what i need : custom_rule: [inventory], But i'm not using :

const myRule = (obj) => {
    // validate stuff
};

To help other peoples with the same issue i will share what i'm using, i'm using custom validation with VeeValidate.Validator.extend('custom_rule', {... and i pass object with : validate(value, obj)
This an example :

VeeValidate.Validator.extend('custom_rule', {
    getMessage: field => 'The '+field+' field is wrong.',
    validate(value, obj) {
        console.log(obj)
        if(value == 'a' || value.length == 0) {
            return true;
        }
        else {
            return false;
        }
    }
});

So i will close this issue. Thank you so much for you help, and I like to specify that as always you answer quickly, I really appreciate that. Thanks.

Can I validate a pdf file using vee-validate?

Hello @logaretm,

These properties in the object which is passed to the "validate" funciton is not "reactive", aren't they?
Do you know how can we make they reactive?

Thanks.

Not working for me. I get [Vue warn]: Error in directive validate bind hook: "TypeError: result is undefined"

Was this page helpful?
0 / 5 - 0 ratings