2

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?

4
  • May you share the code? It's hard to understand what issue you're facing without it. Commented Jan 16, 2020 at 16:38
  • I have added the code 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. 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. Commented Jan 17, 2020 at 16:08

1 Answer 1

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.net/7atc5mwr/

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.