Magento – Magento2: custom validation Error “Uncaught TypeError: Cannot read property ‘call’ of undefined”

custom-validationerrormagento2rules

In my custom validation rules shows '

Uncaught TypeError: Cannot read property 'call' of undefined'
errors!!!…

. how can i do custom logic and proper validation ?

Here is my Code

app/code/{vendor}/{module}/view/frontend/web/template/form/element/email.html

       <input class="input-text"
               type="text"
               data-bind="
                    textInput: email,
                    hasFocus: emailFocused,
                    mageInit: {'mage/trim-input':{}}"
               name="username"
               data-validate="{required:true,'validate-custom-key':true}"
               id="customer-email" />

app/code/{vendor}/{module}/view/frontend/requirejs-config.js

// JavaScript Document
var config = {
    map: {
        '*': {
            script: 'Vendor_Module/js/script'
        },
        '*': {
            'Vendor_Module/js/action/login': 'Vendor_Module/js/action/login'
        },
        '*': {
            'Magento_Checkout/template/form/element/email.html':
                'Vendor_Module/template/form/element/email.html'
        }
    }, mixins: {
        'Magento_Ui/js/lib/validation/validator': {
            'Vendor_Module/js/validator-mixin': true
        }
    }
};

app/design/frontend/Smartwave/porto/web/js/validator-mixin.js

define([
    'jquery'
], function ($) {
    "use strict";

    return function () {
        $.validator.addMethod(
            'validate-custom-key',
            function (value) {
                // Some custom validation stuff here
                return false;
            },
            $.mage.__('Your validation error message')
        );
    }
});

jquery.validate.js:556 Uncaught TypeError: Cannot read property 'call of undefined
enter image description here

Best Answer

Try the following way:

use addRule instead of addMethod.

define([
    'jquery',
    'jquery/validate'
], function ($) {
    "use strict";

    return function (validator) {
        validator.addRule(
            'validate-custom-key',
            function (value) {
                // Some custom validation stuff here
                return false;
            },
            $.mage.__('Your validation error message')
        );
        return validator;
    };
});
Related Topic