Skip to content

Commit

Permalink
Add/Remove mark when region/zip is required (#2149)
Browse files Browse the repository at this point in the history
  • Loading branch information
luigifab authored Sep 27, 2022
1 parent 4a952b0 commit 00748ce
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 41 deletions.
10 changes: 9 additions & 1 deletion app/design/adminhtml/default/default/layout/sales.xml
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,11 @@
<block type="adminhtml/sales_transactions" name="sales_transactions.grid.container"></block>
</reference>
</adminhtml_sales_transactions_index>

<adminhtml_sales_transactions_grid>
<block type="adminhtml/sales_transactions_grid" name="sales_transactions.grid" output="toHtml"></block>
</adminhtml_sales_transactions_grid>

<adminhtml_sales_transactions_view>
<reference name="content">
<block type="adminhtml/sales_transactions_detail" name="sales_transactions.detail" template="sales/transactions/detail.phtml">
Expand Down Expand Up @@ -632,7 +634,6 @@
</reference>
</adminhtml_sales_order_create_load_block_header>


<adminhtml_sales_order_create_load_block_sidebar>
<reference name="content">
<block type="adminhtml/sales_order_create_sidebar" template="sales/order/create/sidebar.phtml" name="sidebar">
Expand Down Expand Up @@ -1051,22 +1052,29 @@
<block type="adminhtml/sales_order_status" name="sales_order_status.grid.container"></block>
</reference>
</adminhtml_sales_order_status_index>

<adminhtml_sales_order_status_new>
<reference name="content">
<block type="adminhtml/sales_order_status_new" name="sales_order_status.new.container"></block>
</reference>
</adminhtml_sales_order_status_new>

<adminhtml_sales_order_status_edit>
<reference name="content">
<block type="adminhtml/sales_order_status_edit" name="sales_order_status.edit.container"></block>
</reference>
</adminhtml_sales_order_status_edit>

<adminhtml_sales_order_status_assign>
<reference name="content">
<block type="adminhtml/sales_order_status_assign" name="sales_order_status.assign.container"></block>
</reference>
</adminhtml_sales_order_status_assign>

<adminhtml_sales_order_address>
<reference name="head">
<block type="adminhtml/template" name="optional_zip_countries" as="optional_zip_countries" template="directory/js/optional_zip_countries.phtml" />
</reference>
<reference name="content">
<block type="adminhtml/sales_order_address" name="sales_order_address.form.container"></block>
</reference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,10 @@ addressesModel.prototype = {
label = $$('label[for="' + currentElement.id + '"]')[0];
if (label) {
wildCard = label.down('em') || label.down('span.required');
if (!wildCard) {
label.insert(' <span class="required">*</span>');
wildCard = label.down('span.required');
}
if (!that.showAllRegions) {
if (regionRequired) {
label.up('tr').show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,5 @@
<script type="text/javascript">
//<![CDATA[
optionalZipCountries = <?php echo $this->helper('directory')->getCountriesWithOptionalZip(true) ?>;

function onAddressCountryChanged (countryElement) {
var zipElementId = countryElement.id.replace(/country_id/, 'postcode');

// Ajax-request and normal content load compatibility
if ($(zipElementId) != undefined) {
setPostcodeOptional($(zipElementId), countryElement.value);
} else {
Event.observe(window, "load", function () {
setPostcodeOptional($(zipElementId), countryElement.value);
});
}
}

function setPostcodeOptional(zipElement, country) {
if (optionalZipCountries.indexOf(country) != -1) {
while (zipElement.hasClassName('required-entry')) {
zipElement.removeClassName('required-entry');
}
zipElement.up(1).down('label > span.required').hide();
} else {
zipElement.addClassName('required-entry');
zipElement.up(1).down('label > span.required').show();
}
}

varienGlobalEvents.attachEventHandler("address_country_changed", onAddressCountryChanged);
//]]>
</script>
36 changes: 36 additions & 0 deletions js/mage/adminhtml/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ RegionUpdater.prototype = {
label = $$('label[for="' + currentElement.id + '"]')[0];
if (label) {
wildCard = label.down('em') || label.down('span.required');
if (!wildCard) {
label.insert(' <span class="required">*</span>');
wildCard = label.down('span.required');
}
var topElement = label.up('tr') || label.up('li');
if (!that.config.show_all_regions && topElement) {
if (regionRequired) {
Expand Down Expand Up @@ -576,3 +580,35 @@ FormElementDependenceController.prototype = {
}
}
};

// optional_zip_countries.phtml
function onAddressCountryChanged(countryElement) {
var zipElementId = countryElement.id.replace(/country_id/, 'postcode');
// Ajax-request and normal content load compatibility
if ($(zipElementId) != undefined) {
setPostcodeOptional($(zipElementId), countryElement.value);
} else {
Event.observe(window, "load", function () {
setPostcodeOptional($(zipElementId), countryElement.value);
});
}
}

function setPostcodeOptional(zipElement, country) {
var spanElement = zipElement.up(1).down('label > span.required');
if (!spanElement || (typeof optionalZipCountries == 'undefined')) {
return; // nothing to do (for example in system config)
}
if (optionalZipCountries.indexOf(country) != -1) {
Validation.reset(zipElement);
while (zipElement.hasClassName('required-entry')) {
zipElement.removeClassName('required-entry');
}
spanElement.hide();
} else {
zipElement.addClassName('required-entry');
spanElement.show();
}
}

varienGlobalEvents.attachEventHandler("address_country_changed", onAddressCountryChanged);
36 changes: 23 additions & 13 deletions js/varien/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @copyright Copyright (c) 2006-2020 Magento, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/

VarienForm = Class.create();
VarienForm.prototype = {
initialize: function(formId, firstFieldFocus){
Expand Down Expand Up @@ -156,8 +157,7 @@ VarienForm.prototype = {

RegionUpdater = Class.create();
RegionUpdater.prototype = {
initialize: function (countryEl, regionTextEl, regionSelectEl, regions, disableAction, zipEl)
{
initialize: function (countryEl, regionTextEl, regionSelectEl, regions, disableAction, zipEl){
this.countryEl = $(countryEl);
this.regionTextEl = $(regionTextEl);
this.regionSelectEl = $(regionSelectEl);
Expand All @@ -176,8 +176,7 @@ RegionUpdater.prototype = {
Event.observe(this.countryEl, 'change', this.update.bind(this));
},

_checkRegionRequired: function()
{
_checkRegionRequired: function(){
var label, wildCard;
var elements = [this.regionTextEl, this.regionSelectEl];
var that = this;
Expand All @@ -191,6 +190,10 @@ RegionUpdater.prototype = {
label = $$('label[for="' + currentElement.id + '"]')[0];
if (label) {
wildCard = label.down('em') || label.down('span.required');
if (!wildCard) {
label.insert(' <span class="required">*</span>');
wildCard = label.down('span.required');
}
if (!that.config.show_all_regions) {
if (regionRequired) {
label.up().show();
Expand Down Expand Up @@ -234,8 +237,7 @@ RegionUpdater.prototype = {
});
},

update: function()
{
update: function(){
if (this.regions[this.countryEl.value]) {
var i, option, region, def;

Expand Down Expand Up @@ -332,7 +334,8 @@ RegionUpdater.prototype = {
}
}
},
sortSelect : function () {

sortSelect: function () {
var elem = this.regionSelectEl;
var tmpArray = new Array();
var currentVal = $(elem).value;
Expand All @@ -356,14 +359,12 @@ RegionUpdater.prototype = {

ZipUpdater = Class.create();
ZipUpdater.prototype = {
initialize: function(country, zipElement)
{
initialize: function(country, zipElement){
this.country = country;
this.zipElement = $(zipElement);
},

update: function()
{
update: function(){
// Country ISO 2-letter codes must be pre-defined
if (typeof optionalZipCountries == 'undefined') {
return false;
Expand All @@ -378,8 +379,7 @@ ZipUpdater.prototype = {
}
},

_setPostcodeOptional: function()
{
_setPostcodeOptional: function(){
this.zipElement = $(this.zipElement);
if (this.zipElement == undefined) {
return false;
Expand All @@ -389,17 +389,27 @@ ZipUpdater.prototype = {
var label = $$('label[for="' + this.zipElement.id + '"]')[0];
if (label != undefined) {
var wildCard = label.down('em') || label.down('span.required');
if (!wildCard) {
label.insert(' <span class="required">*</span>');
wildCard = label.down('span.required');
}
}

// Make Zip and its label required/optional
if (optionalZipCountries.indexOf(this.country) != -1) {
if (label.hasClassName('required')) {
label.removeClassName('required');
}
while (this.zipElement.hasClassName('required-entry')) {
this.zipElement.removeClassName('required-entry');
}
if (wildCard != undefined) {
wildCard.hide();
}
} else {
if (!label.hasClassName('required')) {
label.addClassName('required');
}
this.zipElement.addClassName('required-entry');
if (wildCard != undefined) {
wildCard.show();
Expand Down

0 comments on commit 00748ce

Please sign in to comment.