From b22d96fcfe97c69fe9d29ef7d113a999dceab14f Mon Sep 17 00:00:00 2001 From: Munawar Akbar Date: Fri, 9 Jun 2017 22:17:11 +0100 Subject: [PATCH] New file to perform shipping validation. The chief benefit being that if the website user changes the shipping country then the shipping options will automatically change. --- .../frontend/layout/checkout_cart_index.xml | 30 ++++++++++++++++ .../frontend/layout/checkout_index_index.xml | 35 +++++++++++++++++++ .../model/shipping-rates-validation-rules.js | 15 ++++++++ .../web/js/model/shipping-rates-validator.js | 26 ++++++++++++++ .../web/js/view/shipping-rates-validation.js | 21 +++++++++++ 5 files changed, 127 insertions(+) create mode 100644 src/view/frontend/layout/checkout_cart_index.xml create mode 100644 src/view/frontend/layout/checkout_index_index.xml create mode 100644 src/view/frontend/web/js/model/shipping-rates-validation-rules.js create mode 100644 src/view/frontend/web/js/model/shipping-rates-validator.js create mode 100644 src/view/frontend/web/js/view/shipping-rates-validation.js 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; + } +);