在布局中禁用 magento 结帐页面后,如何在下订单中重新启用名字字段的必填字段验证
How to reenable required field validation for firstname field on place order click in checkout page of magento after disabling it in layout
Magento 2.4
PHP7.4
我需要将名字字段的验证移动到单击“下订单”。这意味着当点击 Place Order 时,应该显示名字字段验证。以前在选择运输方式时会显示验证。我通过修改 checkout_index_index.xml 禁用了该验证,如下面的代码所示。我现在包含了一个额外的支付验证器,我认为它应该在单击“下订单”时生成验证,但那没有发生。我可以触发 javascript 警报弹出窗口进行验证,但这并不理想。
根据我的研究,由于名字字段未嵌入表单中,因此它不会对通过 javascript 添加到其中的必需属性做出反应。
修改checkout_index_index.xml
<item name="shipping-address-fieldset" xsi:type="array">
<item name="component" xsi:type="string">uiComponent</item>
<item name="config" xsi:type="array">
<item name="deps" xsi:type="array">
<item name="0" xsi:type="string">checkoutProvider</item>
</item>
</item>
<item name="displayArea" xsi:type="string">additional-fieldsets</item>
<item name="children" xsi:type="array">
<item name="firstname" xsi:type="array">
<item name="config" xsi:type="array">
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">false</item>
</item>
</item>
</item>
额外的支付验证器 -
<item name="additional-payment-validators" xsi:type="array">
<item name="component" xsi:type="string">uiComponent</item>
<item name="children" xsi:type="array">
<!-- merge payment validators here -->
<item name="email-validator" xsi:type="array">
<item name="component" xsi:type="string">Magento_Checkout/js/view/payment/email-validator</item>
</item>
<item name="address-validator" xsi:type="array">
<item name="component" xsi:type="string">Magento_Checkout/js/view/shipping-address/address-validator</item>
</item>
地址验证器(查看)-
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define(
[
'uiComponent',
'Magento_Checkout/js/model/payment/additional-validators',
'Magento_Checkout/js/model/default-validator'
],
function (Component, additionalValidators, defaultValidator) {
'use strict';
console.log(defaultValidator);
additionalValidators.registerValidator(defaultValidator);
console.log("inside address validator");
return Component.extend({});
}
);
默认验证器(模型)-
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([
'jquery',
'mageUtils',
'./default-validation-rules',
'mage/translate'
], function ($, utils, validationRules, $t) {
'use strict';
return {
validationErrors: [],
/**
* @param {Object} address
* @return {Boolean}
*/
validate: function (address) {
var self = this;
this.validationErrors = [];
$.each(validationRules.getRules(), function (field, rule) {
var message;
if (rule.required && utils.isEmpty(address[field])) {
message = $t('Field ') + field + $t(' is required.');
self.validationErrors.push(message);
}
});
return !this.validationErrors.length;
}
};
});
验证规则-
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([], function () {
'use strict';
return {
/**
* @return {Object}
*/
getRules: function () {
return {
'country_id': {
'required': true
},
'firstname': {
'required': true
}
};
}
};
});
通过联系 Amasty 支持找到了答案 - 需要关闭动态验证 (shipping-mixin.js)。
Magento 2.4 PHP7.4
我需要将名字字段的验证移动到单击“下订单”。这意味着当点击 Place Order 时,应该显示名字字段验证。以前在选择运输方式时会显示验证。我通过修改 checkout_index_index.xml 禁用了该验证,如下面的代码所示。我现在包含了一个额外的支付验证器,我认为它应该在单击“下订单”时生成验证,但那没有发生。我可以触发 javascript 警报弹出窗口进行验证,但这并不理想。
根据我的研究,由于名字字段未嵌入表单中,因此它不会对通过 javascript 添加到其中的必需属性做出反应。
修改checkout_index_index.xml
<item name="shipping-address-fieldset" xsi:type="array">
<item name="component" xsi:type="string">uiComponent</item>
<item name="config" xsi:type="array">
<item name="deps" xsi:type="array">
<item name="0" xsi:type="string">checkoutProvider</item>
</item>
</item>
<item name="displayArea" xsi:type="string">additional-fieldsets</item>
<item name="children" xsi:type="array">
<item name="firstname" xsi:type="array">
<item name="config" xsi:type="array">
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">false</item>
</item>
</item>
</item>
额外的支付验证器 -
<item name="additional-payment-validators" xsi:type="array">
<item name="component" xsi:type="string">uiComponent</item>
<item name="children" xsi:type="array">
<!-- merge payment validators here -->
<item name="email-validator" xsi:type="array">
<item name="component" xsi:type="string">Magento_Checkout/js/view/payment/email-validator</item>
</item>
<item name="address-validator" xsi:type="array">
<item name="component" xsi:type="string">Magento_Checkout/js/view/shipping-address/address-validator</item>
</item>
地址验证器(查看)-
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define(
[
'uiComponent',
'Magento_Checkout/js/model/payment/additional-validators',
'Magento_Checkout/js/model/default-validator'
],
function (Component, additionalValidators, defaultValidator) {
'use strict';
console.log(defaultValidator);
additionalValidators.registerValidator(defaultValidator);
console.log("inside address validator");
return Component.extend({});
}
);
默认验证器(模型)-
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([
'jquery',
'mageUtils',
'./default-validation-rules',
'mage/translate'
], function ($, utils, validationRules, $t) {
'use strict';
return {
validationErrors: [],
/**
* @param {Object} address
* @return {Boolean}
*/
validate: function (address) {
var self = this;
this.validationErrors = [];
$.each(validationRules.getRules(), function (field, rule) {
var message;
if (rule.required && utils.isEmpty(address[field])) {
message = $t('Field ') + field + $t(' is required.');
self.validationErrors.push(message);
}
});
return !this.validationErrors.length;
}
};
});
验证规则-
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([], function () {
'use strict';
return {
/**
* @return {Object}
*/
getRules: function () {
return {
'country_id': {
'required': true
},
'firstname': {
'required': true
}
};
}
};
});
通过联系 Amasty 支持找到了答案 - 需要关闭动态验证 (shipping-mixin.js)。