扩展Magento 2 rules.js而不覆盖核心js文件?

时间:2017-07-07 13:23:11

标签: validation magento magento2 extend

是否可以覆盖Magento_Ui / js / lib / validation / rules.js文件而无需映射文件,如下例所示?

a        |       b
100      | Rock+Y;Paper+No;Scissor+Yes;Dragon+Y;<br>
101      | Rock+N;Paper+No;Scissor+Yes;Dragon+N;<br>
102      | Rock+Y;Paper+Yes;Scissor+N;Dragon+Y;

是否有更优雅的方式来添加可在结帐步骤中使用的表单验证?

由于

1 个答案:

答案 0 :(得分:0)

在 Vendor/Module/view/adminhtml/ 中创建 requirejs-config.js

var config = {
config: {
    mixins: {
        'Magento_Ui/js/lib/validation/rules': {
            'Vendor_Module/js/lib/validation/rules': true
           }
         }
      }
 };

在 Vendor/Module/view/base/web/js/lib/validation 中创建 rules.js

define([
'jquery',
'underscore',
'moment',
'mage/translate'
], function ($, _, moment) {
'use strict';

return function (validator) {
    var validators = {
        'validate-number-custom': [
            function (value) {
                console.log("You logic here");
                return /^[a-zA-Z ]*$/i.test(value);
            },
            $.mage.__('Please enter a valid number in this field.')
        ]
    };

    validators = _.mapObject(validators, function (data) {
        return {
            handler: data[0],
            message: data[1]
        };
    });

    return $.extend(validator, validators);
  };
});