最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论