在结帐字段国家/地区添加自定义验证
Adding custom validation on checkout field country
我需要在所有可以使用国家字段的地方添加验证。
对于注册或地址编辑它工作正常但在结帐时我尝试了几种方法但没有任何效果。我想要在新送货地址表单的字段上进行验证。
- 我在 js 文件中添加验证
countryValidation.js
:
用于注册或编辑地址的相同脚本并且工作正常
define([
'jquery',
'jquery/ui',
'mage/validation',
'mage/translate',
'domReady!'
], function($){
'use strict';
return function(validator) {
$.validator.addMethod(
"validate-country",
function(value, element) {
if (value === "FR") {
var zipValue = $('input[name="postcode"]').val();
if (zipValue) {
return !(zipValue.startsWith("97") || zipValue.startsWith("98"));
}
}
return true;
},
$.mage.__("You cannot choose France for DOM-TOM Zip Code")
);
return validator;
}
});
- 我在模块的
requirejs-config.js
中注册了它:
var config = {
config: {
mixins: {
'Magento_Ui/js/lib/validation/validator': {
'Gone_Customer/js/countryValidation': true
}
}
}
};
- 为了向结帐方法添加验证,我尝试了不同的方法
-> 方法 A:使用插件
class AddCountryValidation
{
public function afterProcess(
\Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
array $jsLayout
) {
// Country ID
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['country_id']['validation']['validate-country'] = true;
return $jsLayout;
}
}
-> 方法 B:在属性中添加验证规则
在 customer_eav_attribute
的 country_id
属性中 validate_rules
我添加了 {"validate-country": true}
当我验证表单时,我没有验证错误,而我应该有一个。
如果我遗漏了什么,你能告诉我吗?
请试试这个
define([
'jquery',
'jquery/validate'
], function($){
'use strict';
return function(validator) {
validator.addRule(
"validate-country",
function(value) {
if (value === "FR") {
var zipValue = $('input[name="postcode"]').val();
if (zipValue) {
return !(zipValue.startsWith("97") || zipValue.startsWith("98"));
}
}
return false;
},
$.mage.__("You cannot choose France for DOM-TOM Zip Code")
);
return validator;
}
});
我用过return false 你可以根据你的need.Tested修改2.2.2
版本
感谢您的回复,同时我发现了一些有用的东西。
我必须只为结帐做一个单独的 js 验证:
define(['mage/translate', "jquery"], function($t, $) {
'use strict';
return function(rules) {
rules['validate-country'] = {
handler: function (value) {
if (value === "FR") {
var zipValue = $('input[name="postcode"]').val();
if (zipValue) {
return !(zipValue.startsWith("97") || zipValue.startsWith("98"));
}
}
return true;
},
message: $t('You cannot choose France for DOM-TOM Zip Code')
};
return rules;
};
});
并且在我的 mixin 中,我将 Magento_Ui/js/lib/validation/validator
更改为 Magento_Ui/js/lib/validation/rules
然后我的插件方法开始工作了!
我需要在所有可以使用国家字段的地方添加验证。
对于注册或地址编辑它工作正常但在结帐时我尝试了几种方法但没有任何效果。我想要在新送货地址表单的字段上进行验证。
- 我在 js 文件中添加验证
countryValidation.js
:
用于注册或编辑地址的相同脚本并且工作正常
define([
'jquery',
'jquery/ui',
'mage/validation',
'mage/translate',
'domReady!'
], function($){
'use strict';
return function(validator) {
$.validator.addMethod(
"validate-country",
function(value, element) {
if (value === "FR") {
var zipValue = $('input[name="postcode"]').val();
if (zipValue) {
return !(zipValue.startsWith("97") || zipValue.startsWith("98"));
}
}
return true;
},
$.mage.__("You cannot choose France for DOM-TOM Zip Code")
);
return validator;
}
});
- 我在模块的
requirejs-config.js
中注册了它:
var config = {
config: {
mixins: {
'Magento_Ui/js/lib/validation/validator': {
'Gone_Customer/js/countryValidation': true
}
}
}
};
- 为了向结帐方法添加验证,我尝试了不同的方法
-> 方法 A:使用插件
class AddCountryValidation
{
public function afterProcess(
\Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
array $jsLayout
) {
// Country ID
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['country_id']['validation']['validate-country'] = true;
return $jsLayout;
}
}
-> 方法 B:在属性中添加验证规则
在 customer_eav_attribute
的 country_id
属性中 validate_rules
我添加了 {"validate-country": true}
当我验证表单时,我没有验证错误,而我应该有一个。
如果我遗漏了什么,你能告诉我吗?
请试试这个
define([
'jquery',
'jquery/validate'
], function($){
'use strict';
return function(validator) {
validator.addRule(
"validate-country",
function(value) {
if (value === "FR") {
var zipValue = $('input[name="postcode"]').val();
if (zipValue) {
return !(zipValue.startsWith("97") || zipValue.startsWith("98"));
}
}
return false;
},
$.mage.__("You cannot choose France for DOM-TOM Zip Code")
);
return validator;
}
});
我用过return false 你可以根据你的need.Tested修改2.2.2
版本感谢您的回复,同时我发现了一些有用的东西。 我必须只为结帐做一个单独的 js 验证:
define(['mage/translate', "jquery"], function($t, $) {
'use strict';
return function(rules) {
rules['validate-country'] = {
handler: function (value) {
if (value === "FR") {
var zipValue = $('input[name="postcode"]').val();
if (zipValue) {
return !(zipValue.startsWith("97") || zipValue.startsWith("98"));
}
}
return true;
},
message: $t('You cannot choose France for DOM-TOM Zip Code')
};
return rules;
};
});
并且在我的 mixin 中,我将 Magento_Ui/js/lib/validation/validator
更改为 Magento_Ui/js/lib/validation/rules
然后我的插件方法开始工作了!