diff --git a/src/view/frontend/layout/checkout_cart_index.xml b/src/view/frontend/layout/checkout_cart_index.xml new file mode 100644 index 0000000..115c3d9 --- /dev/null +++ b/src/view/frontend/layout/checkout_cart_index.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + Meanbee_RoyalMail/js/view/shipping-rates-validation + + + + + + + + + + + diff --git a/src/view/frontend/layout/checkout_index_index.xml b/src/view/frontend/layout/checkout_index_index.xml new file mode 100644 index 0000000..a09dcf0 --- /dev/null +++ b/src/view/frontend/layout/checkout_index_index.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + Meanbee_RoyalMail/js/view/shipping-rates-validation + + + + + + + + + + + + + + + + + diff --git a/src/view/frontend/web/js/model/shipping-rates-validation-rules.js b/src/view/frontend/web/js/model/shipping-rates-validation-rules.js new file mode 100644 index 0000000..2ddae5c --- /dev/null +++ b/src/view/frontend/web/js/model/shipping-rates-validation-rules.js @@ -0,0 +1,15 @@ +define( + [], + function () { + 'use strict'; + return { + getRules: function() { + return { + 'country_id': { + 'required': true + } + }; + } + }; + } +) diff --git a/src/view/frontend/web/js/model/shipping-rates-validator.js b/src/view/frontend/web/js/model/shipping-rates-validator.js new file mode 100644 index 0000000..db8aeb0 --- /dev/null +++ b/src/view/frontend/web/js/model/shipping-rates-validator.js @@ -0,0 +1,26 @@ +define( + [ + 'jquery', + 'mageUtils', + + './shipping-rates-validation-rules', + 'mage/translate' + ], + function ($, utils, validationRules, $t) { + 'use strict'; + return { + validationErrors: [], + validate: function(address) { + var self = this; + this.validationErrors = []; + $.each(validationRules.getRules(), function(field, rule) { + if (rule.required && utils.isEmpty(address[field])) { + var message = $t('Field ') + field + $t(' is required.'); + self.validationErrors.push(message); + } + }); + return !Boolean(this.validationErrors.length); + } + }; + } +); diff --git a/src/view/frontend/web/js/view/shipping-rates-validation.js b/src/view/frontend/web/js/view/shipping-rates-validation.js new file mode 100644 index 0000000..ef7a592 --- /dev/null +++ b/src/view/frontend/web/js/view/shipping-rates-validation.js @@ -0,0 +1,21 @@ +define( + [ + 'uiComponent', + 'Magento_Checkout/js/model/shipping-rates-validator', + 'Magento_Checkout/js/model/shipping-rates-validation-rules', + '../model/shipping-rates-validator', + '../model/shipping-rates-validation-rules' + ], + function ( + Component, + defaultShippingRatesValidator, + defaultShippingRatesValidationRules, + shippingRatesValidator, + shippingRatesValidationRules + ) { + 'use strict'; + defaultShippingRatesValidator.registerValidator('rm', shippingRatesValidator); + defaultShippingRatesValidationRules.registerRules('rm', shippingRatesValidationRules); + return Component; + } +);