是否可以在不映射的情况下覆盖 Magento_Ui/js/lib/validation/rules.js 文件 - 如下例所示?
var config = {
"map": {
"*" : {
'Magento_Ui/js/lib/validation/rules' : 'Test_Custom/js/validation/rules'
}
}
};
是否有更优雅的方法来添加可在结帐步骤中使用的表单验证?
谢谢
最佳答案
在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);
};
});
关于validation - 在不覆盖核心 js 文件的情况下扩展 Magento 2 rules.js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44971910/