We can display multiple errors in different sections by just defining the error function once to optimize the code.
events: _.extend(OrderWizardModuleAddress.prototype.events, {
'click [data-action="select"]': "selectAddress",
}),
// function to send the error details to the showMessage function
showError: function (message, type, container) {
this.showMessage(message, type, container);
},
// function to display the error
showMessage: function (message, type, container) {
var global_view_message = new GlobalViewsMessageView({
message: message,
type: type,
closable: true,
});
jQuery(container).show();
var msgContainerParent = jQuery(container);
msgContainerParent.html(global_view_message.render().$el.html());
},
//this code is to check the po box is there or not in the address//
selectAddress: function (e) {
let self = this;
jQuery(".wizard-content .alert-error").hide();
let selectedAddr1 = this.addresses._byId[jQuery(e.target).data("id").toString()].attributes.addr1;
let poBoxPattern = /^ *((#\d+)|((box|bin)[-. \/\\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\\]? *-?((box|bin)|b|(#|n|num|number)?\d+))|(p(ost|ostal)? *(o(ff(ice)?)?)? *((box|bin)|b)? *(#|n|num|number)*\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(#|n|num|number)? *\d+|(#|n|num|number) *\d+)/i;
if ( this.$el[0].attributes[0].value.indexOf( "OrderWizard.Module.Address.Shipping" ) > -1) {
if (poBoxPattern.test(selectedAddr1)) {
self.showError('You cannot select a PO Box address as Shipping Address', 'error', '.order-wizard-shipmethod-module-message-error');
this.error = null;
$("html, body").animate({
scrollTop: $('.order-wizard-shipmethod-module-message-error').offset().top - 70}, 10);
} else {
this.setAddress(jQuery(e.target).data("id").toString());
this.render();
this.trigger("ready", true);
}
} else {
this.setAddress(jQuery(e.target).data("id").toString());
this.render();
this.trigger("ready", true);
}
},