javascript - Vuelidate custom validation function in Vue.js - Stack Overflow

I am using vuelidate to implement validation and trying to access whole data object from custom functio

I am using vuelidate to implement validation and trying to access whole data object from custom function (I have read that 2nd parameter takes the data object), but it is only getting observer and it has only the data of same level in hierarchy.

I have applied custom validation on x11, then I am getting only x11 and x12 in 2nd parameter, not the whole object.

customFunction(value, wholeObject)
{
console.log(value); //value of x11
console.log(wholeObject); // it is printing observer x11 and x12. I was         
expecting //it will print the whole x object
}

data: {
    x: {
        x1: {
            x11,
            x12
        },
        x2
    }
},

validations: {
    x: {
        x1: {
            x11: CustomFunction,
            x12
        },
        x2
    }
}

Is it the correct behavior or am I doing something wrong?

I am using vuelidate to implement validation and trying to access whole data object from custom function (I have read that 2nd parameter takes the data object), but it is only getting observer and it has only the data of same level in hierarchy.

I have applied custom validation on x11, then I am getting only x11 and x12 in 2nd parameter, not the whole object.

customFunction(value, wholeObject)
{
console.log(value); //value of x11
console.log(wholeObject); // it is printing observer x11 and x12. I was         
expecting //it will print the whole x object
}

data: {
    x: {
        x1: {
            x11,
            x12
        },
        x2
    }
},

validations: {
    x: {
        x1: {
            x11: CustomFunction,
            x12
        },
        x2
    }
}

Is it the correct behavior or am I doing something wrong?

Share Improve this question edited Jan 17, 2020 at 6:28 Ajay Agrawal asked Jan 16, 2020 at 16:37 Ajay AgrawalAjay Agrawal 1551 gold badge2 silver badges11 bronze badges 4
  • May you share the code? It's hard to understand what issue you're facing without it. – evolutionxbox Commented Jan 16, 2020 at 16:38
  • I have added the code – Ajay Agrawal Commented Jan 17, 2020 at 6:53
  • I can't see where you are using the custom function. So at the moment your code is fine. – evolutionxbox Commented Jan 17, 2020 at 9:27
  • I am applying validation on x11 by calling CustomFunction. You can see it in validations attribute. I want to get whole data object in that function. – Ajay Agrawal Commented Jan 17, 2020 at 16:08
Add a ment  | 

1 Answer 1

Reset to default 3

Can you try using following code:

function customFunction(value) {
    console.log(value);
    console.log(this);
    return value != '';
}

data: {     
    x: {
        x1: { x11: 'abc', x12: 'pqr'},
        x2: 'lmn'
    }
}

validations: {
    x: {
        x1: {
            x11: CustomFunction,
            x12: required
        },
        x2: required
    }
}

Fiddle -> https://jsfiddle/7atc5mwr/

Please read this page to understand how to use custom validators along and access ponent.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744899991a4599919.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信