简体   繁体   English

Vue.js 中的 Vuelidate 自定义验证功能

[英]Vuelidate custom validation function in Vue.js

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.我正在使用 vuelidate 来实现验证并尝试从自定义函数访问整个数据对象(我已经读过第二个参数采用数据对象),但它只是获取观察者,并且它只有层次结构中相同级别的数据。

I have applied custom validation on x11, then I am getting only x11 and x12 in 2nd parameter, not the whole object.我已经在 x11 上应用了自定义验证,然后我在第二个参数中只得到了 x11 和 x12,而不是整个对象。

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?这是正确的行为还是我做错了什么?

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.net/7atc5mwr/小提琴 -> https://jsfiddle.net/7atc5mwr/

Please read this page to understand how to use custom validators along and access component.请阅读此页面以了解如何使用自定义验证器和访问组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM