简体   繁体   中英

How can I pass a param to a custom validator?

I have the below custom validator, which is working great when passed to my input like this:

<select id="salesOwnerIdInput" v-model="currentCompany.salesOwnerId" name="salesOwnerId"
        v-validate.immediate="{ 'required': salesOwnerRequired, 'contains': !userHasPlanAccess }"
        :class="{'form-control': true, 'is-invalid': errors.has('main-tab.salesOwnerId') }"
        data-vv-as="Sales Owner" 
        data-vv-scope="main-tab">
    <option value="">- Select -</option>
    <option v-for="option in ownerList" v-bind:value="option.id" v-bind:key="option.id">
        {{ option.name }}
    </option>
</select>

But as soon as I try to add a param

v-validate.immediate="{ 'required': salesOwnerRequired, 'contains:test': !userHasPlanAccess }"

I get this stupid error

vee-validate.esm.js:290 Uncaught (in promise) Error: [vee-validate] No such validator 'contains:test' exists.

What am I doing wrong??

const ContainsValidator = {
    params: [
        'message'
    ],
    getMessage(field, args) {
        return 'get message from args';
    },
    validate(value, args) {
        return false;
    }
}

I tried adding the param name to my registration, but it has no effect - I'm not sure if this even makes sense?

VeeValidate.Validator.extend("contains", ContainsValidator, { paramNames: ['message'] });

I am using vee-validate 2.1.4

I managed to solve it by calling the validator like this

v-validate.immediate="`required:${salesOwnerRequired}|contains:testMessage,${salesOwnerHasPlanAccess}`"

So passed the entire ruleset as a string, and interpolated anything that needed to be passed down

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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