Skip to content

Fix #25350 - Some JS removes State/Province field for United Kingdom for example #25386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions app/code/Magento/Checkout/view/frontend/web/js/region-updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ define([
isZipRequired: true,
isCountryRequired: true,
currentRegion: null,
currentRegionValue: null,
isMultipleCountriesAllowed: true
},

Expand All @@ -30,19 +31,34 @@ define([
* @private
*/
_create: function () {
var regionInput = $(this.options.regionInputId);

this._initCountryElement();

this.currentRegionOption = this.options.currentRegion;
this.regionTmpl = mageTemplate(this.options.regionTemplate);
this.currentRegionOption = {};
this.currentRegionValue = {};

if (this.options.currentRegion) {
this.currentRegionOption[this.element.val()] = this.options.currentRegion;
}

if (this.options.currentRegionValue) {
this.currentRegionValue[this.element.val()] = this.options.currentRegionValue;
}

this._updateRegion(this.element.find('option:selected').val());

$(this.options.regionListId).on('change', $.proxy(function (e) {
this.setOption = false;
this.currentRegionOption = $(e.target).val();
this.currentRegionOption[this.element.val()] = $(e.target).val();
}, this));

regionInput.on('change', $.proxy(function (e) {
this.currentRegionValue[this.element.val()] = $(e.target).val();
}, this));

$(this.options.regionInputId).on('focusout', $.proxy(function () {
regionInput.on('focusout', $.proxy(function () {
this.setOption = true;
}, this));
},
Expand Down Expand Up @@ -161,22 +177,17 @@ define([

this._clearError();
this._checkRegionRequired(country);

$(regionList).find('option:selected').removeAttr('selected');
regionInput.val('');
this._removeSelectOptions(regionList);

// Populate state/province dropdown list if available or use input box
if (this.options.regionJson[country]) {
this._removeSelectOptions(regionList);
$.each(this.options.regionJson[country], $.proxy(function (key, value) {
this._renderSelectOption(regionList, key, value);
}, this));

if (this.currentRegionOption) {
regionList.val(this.currentRegionOption);
}

if (this.setOption) {
if (this.currentRegionOption[country]) {
regionList.val(this.currentRegionOption[country]);
} else if (this.setOption) {
regionList.find('option').filter(function () {
return this.text === regionInput.val();
}).attr('selected', true);
Expand All @@ -201,8 +212,6 @@ define([
regionInput.hide();
label.attr('for', regionList.attr('id'));
} else {
this._removeSelectOptions(regionList);

if (this.options.isRegionRequired) {
regionInput.addClass('required-entry').removeAttr('disabled');
container.addClass('required').show();
Expand All @@ -215,6 +224,12 @@ define([
regionInput.removeClass('required-entry');
}

if (this.currentRegionValue[country]) {
regionInput.val(this.currentRegionValue[country]);
} else {
regionInput.val('');
}

regionList.removeClass('required-entry').prop('disabled', 'disabled').hide();
regionInput.show();
label.attr('for', regionInput.attr('id'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<block class="Magento\Customer\Block\Form\Register" name="customer_form_register" template="Magento_Customer::form/register.phtml">
<arguments>
<argument name="attribute_data" xsi:type="object">Magento\Customer\Block\DataProviders\AddressAttributeData</argument>
<argument name="view_model" xsi:type="object">Magento\Customer\ViewModel\Address</argument>
</arguments>
<container name="form.additional.info" as="form_additional_info"/>
<container name="customer.form.register.fields.before" as="form_fields_before" label="Form Fields Before" htmlTag="div" htmlClass="customer-form-before"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ $viewModel = $block->getViewModel();
"postcodeId": "#zip",
"form": "#form-validate",
"regionJson": <?= /* @noEscape */ $viewModel->dataGetRegionJson() ?>,
"currentRegionValue": "<?= /* @noEscape */ $block->getRegion() ?>",
"defaultRegion": "<?= (int) $block->getRegionId() ?>",
"countriesWithOptionalZip": <?= /* @noEscape */ $viewModel->dataGetCountriesWithOptionalZip(true) ?>
}
Expand Down
131 changes: 71 additions & 60 deletions app/code/Magento/Customer/view/frontend/templates/form/register.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// phpcs:disable Magento2.Templates.ThisInTemplate.FoundHelper
// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis

use Magento\Customer\Helper\Address;
use Magento\Customer\Block\Form\Register;
use Magento\Customer\Block\Widget\Company;
use Magento\Customer\Block\Widget\Dob;
use Magento\Customer\Block\Widget\Fax;
use Magento\Customer\Block\Widget\Gender;
use Magento\Customer\Block\Widget\Name;
use Magento\Customer\Block\Widget\Taxvat;
use Magento\Customer\Block\Widget\Telephone;
use Magento\Customer\ViewModel\Address;
use Magento\Framework\Escaper;

/** @var \Magento\Customer\Block\Form\Register $block */
/** @var \Magento\Framework\Escaper $escaper */
/**
* @var Register $block
* @var Escaper $escaper
* @var Address $viewModel
*/
$formData = $block->getFormData();
$viewModel = $block->getViewModel();
?>
<?php $displayAll = $block->getConfig('general/region/display_all'); ?>
<?= $block->getChildHtml('form_fields_before') ?>
<?php /* Extensions placeholder */ ?>
<?= $block->getChildHtml('customer.form.register.extra') ?>
Expand All @@ -27,11 +37,13 @@ $formData = $block->getFormData();
<legend class="legend"><span><?= $escaper->escapeHtml(__('Personal Information')) ?></span></legend><br>
<input type="hidden" name="success_url" value="<?= $escaper->escapeUrl($block->getSuccessUrl()) ?>">
<input type="hidden" name="error_url" value="<?= $escaper->escapeUrl($block->getErrorUrl()) ?>">
<?= $block->getLayout()
->createBlock(\Magento\Customer\Block\Widget\Name::class)
<?=
$block->getLayout()
->createBlock(Name::class)
->setObject($formData)
->setForceUseCustomerAttributes(true)
->toHtml() ?>
->toHtml()
?>
<?php if ($block->isNewsletterEnabled()): ?>
<div class="field choice newsletter">
<input type="checkbox"
Expand All @@ -49,77 +61,76 @@ $formData = $block->getFormData();
<?= $block->getChildHtml('customer.form.register.newsletter') ?>
<?php endif ?>

<?php $_dob = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Dob::class) ?>
<?php if ($_dob->isEnabled()): ?>
<?= $_dob->setDate($formData->getDob())->toHtml() ?>
<?php $dob = $block->getLayout()->createBlock(Dob::class) ?>
<?php if ($dob->isEnabled()): ?>
<?= $dob->setDate($formData->getDob())->toHtml() ?>
<?php endif ?>

<?php $_taxvat = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Taxvat::class) ?>
<?php if ($_taxvat->isEnabled()): ?>
<?= $_taxvat->setTaxvat($formData->getTaxvat())->toHtml() ?>
<?php $taxvat = $block->getLayout()->createBlock(Taxvat::class) ?>
<?php if ($taxvat->isEnabled()): ?>
<?= $taxvat->setTaxvat($formData->getTaxvat())->toHtml() ?>
<?php endif ?>

<?php $_gender = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Gender::class) ?>
<?php if ($_gender->isEnabled()): ?>
<?= $_gender->setGender($formData->getGender())->toHtml() ?>
<?php $gender = $block->getLayout()->createBlock(Gender::class) ?>
<?php if ($gender->isEnabled()): ?>
<?= $gender->setGender($formData->getGender())->toHtml() ?>
<?php endif ?>
</fieldset>
<?php if ($block->getShowAddressFields()): ?>
<?php $cityValidationClass = $this->helper(Address::class)->getAttributeValidationClass('city'); ?>
<?php $postcodeValidationClass = $this->helper(Address::class)->getAttributeValidationClass('postcode'); ?>
<?php $regionValidationClass = $this->helper(Address::class)->getAttributeValidationClass('region'); ?>
<?php $countryIdLabel = $block->getAttributeData()->getFrontendLabel('country_id'); ?>
<?php $streetLabel = $block->getAttributeData()->getFrontendLabel('street'); ?>
<?php $cityLabel = $block->getAttributeData()->getFrontendLabel('city'); ?>
<?php $regionLabel = $block->getAttributeData()->getFrontendLabel('region'); ?>

<?php $cityValidationClass = $viewModel->addressGetAttributeValidationClass('city'); ?>
<?php $postcodeValidationClass = $viewModel->addressGetAttributeValidationClass('postcode'); ?>
<?php $regionValidationClass = $viewModel->addressGetAttributeValidationClass('region'); ?>
<?php $streetValidationClass = $viewModel->addressGetAttributeValidationClass('street'); ?>
<?php $streetValidationClassNotRequired = trim(str_replace('required-entry', '', $streetValidationClass)); ?>
<fieldset class="fieldset address">
<legend class="legend"><span><?= $escaper->escapeHtml(__('Address Information')) ?></span></legend><br>
<input type="hidden" name="create_address" value="1" />

<?php $_company = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Company::class) ?>
<?php if ($_company->isEnabled()): ?>
<?= $_company->setCompany($formData->getCompany())->toHtml() ?>
<?php $company = $block->getLayout()->createBlock(Company::class) ?>
<?php if ($company->isEnabled()): ?>
<?= $company->setCompany($formData->getCompany())->toHtml() ?>
<?php endif ?>

<?php $_telephone = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Telephone::class) ?>
<?php if ($_telephone->isEnabled()): ?>
<?= $_telephone->setTelephone($formData->getTelephone())->toHtml() ?>
<?php $telephone = $block->getLayout()->createBlock(Telephone::class) ?>
<?php if ($telephone->isEnabled()): ?>
<?= $telephone->setTelephone($formData->getTelephone())->toHtml() ?>
<?php endif ?>

<?php $_fax = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Fax::class) ?>
<?php if ($_fax->isEnabled()): ?>
<?= $_fax->setFax($formData->getFax())->toHtml() ?>
<?php $fax = $block->getLayout()->createBlock(Fax::class) ?>
<?php if ($fax->isEnabled()): ?>
<?= $fax->setFax($formData->getFax())->toHtml() ?>
<?php endif ?>

<?php
$_streetValidationClass = $this->helper(Address::class)
->getAttributeValidationClass('street');
?>

<div class="field street required">
<label for="street_1" class="label">
<span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('street') ?></span>
<span><?= /* @noEscape */ $streetLabel ?></span>
</label>
<div class="control">
<input type="text"
name="street[]"
value="<?= $escaper->escapeHtmlAttr($formData->getStreet(0)) ?>"
title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('street') ?>"
title="<?= /* @noEscape */ $streetLabel ?>"
id="street_1"
class="input-text <?= $escaper->escapeHtmlAttr($_streetValidationClass) ?>">
class="input-text <?= $escaper->escapeHtmlAttr($streetValidationClass) ?>">
<div class="nested">
<?php
$_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass));
$streetLines = $this->helper(Address::class)->getStreetLines();
?>
<?php for ($_i = 2, $_n = $streetLines; $_i <= $_n; $_i++): ?>
<?php for ($i = 1, $n = $viewModel->addressGetStreetLines(); $i < $n; $i++): ?>
<div class="field additional">
<label class="label" for="street_<?= /* @noEscape */ $_i ?>">
<label class="label" for="street_<?= /* @noEscape */ $i ?>">
<span><?= $escaper->escapeHtml(__('Address')) ?></span>
</label>
<div class="control">
<input type="text"
name="street[]"
value="<?= $escaper->escapeHtml($formData->getStreetLine($_i - 1)) ?>"
title="<?= $escaper->escapeHtmlAttr(__('Street Address %1', $_i)) ?>"
id="street_<?= /* @noEscape */ $_i ?>"
class="input-text <?= $escaper->escapeHtmlAttr($_streetValidationClass) ?>">
value="<?= $escaper->escapeHtml($formData->getStreetLine($i - 1)) ?>"
title="<?= $escaper->escapeHtmlAttr(__('Street Address %1', $i)) ?>"
id="street_<?= /* @noEscape */ $i ?>"
class="input-text
<?= $escaper->escapeHtmlAttr($streetValidationClassNotRequired) ?>">
</div>
</div>
<?php endfor; ?>
Expand All @@ -129,7 +140,7 @@ $formData = $block->getFormData();

<div class="field country required">
<label for="country" class="label">
<span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('country_id') ?></span>
<span><?= /* @noEscape */ $countryIdLabel ?></span>
</label>
<div class="control">
<?= $block->getCountryHtmlSelect() ?>
Expand All @@ -138,12 +149,12 @@ $formData = $block->getFormData();

<div class="field region required">
<label for="region_id" class="label">
<span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?></span>
<span><?= /* @noEscape */ $regionLabel ?></span>
</label>
<div class="control">
<select id="region_id"
name="region_id"
title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>"
title="<?= /* @noEscape */ $regionLabel ?>"
class="validate-select region_id"
style="display: none;">
<option value="">
Expand All @@ -154,21 +165,21 @@ $formData = $block->getFormData();
id="region"
name="region"
value="<?= $escaper->escapeHtml($block->getRegion()) ?>"
title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>"
title="<?= /* @noEscape */ $regionLabel ?>"
class="input-text <?= $escaper->escapeHtmlAttr($regionValidationClass) ?>"
style="display:none;">
</div>
</div>

<div class="field required">
<label for="city" class="label">
<span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('city') ?></span>
<span><?= /* @noEscape */ $cityLabel ?></span>
</label>
<div class="control">
<input type="text"
name="city"
value="<?= $escaper->escapeHtmlAttr($formData->getCity()) ?>"
title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('city') ?>"
title="<?= /* @noEscape */ $cityLabel ?>"
class="input-text <?= $escaper->escapeHtmlAttr($cityValidationClass) ?>"
id="city">
</div>
Expand Down Expand Up @@ -280,10 +291,10 @@ require([
], function($){

var dataForm = $('#form-validate');
var ignore = <?= /* @noEscape */ $_dob->isEnabled() ? '\'input[id$="full"]\'' : 'null' ?>;
var ignore = <?= /* @noEscape */ $dob->isEnabled() ? '\'input[id$="full"]\'' : 'null' ?>;

dataForm.mage('validation', {
<?php if ($_dob->isEnabled()): ?>
<?php if ($dob->isEnabled()): ?>
errorPlacement: function(error, element) {
if (element.prop('id').search('full') !== -1) {
var dobElement = $(element).parents('.customer-dob'),
Expand All @@ -305,6 +316,7 @@ require([
});
</script>
<?php if ($block->getShowAddressFields()): ?>
<?php $displayAll = $block->getConfig('general/region/display_all'); ?>
<script type="text/x-magento-init">
{
"#country": {
Expand All @@ -314,11 +326,10 @@ require([
"regionInputId": "#region",
"postcodeId": "#zip",
"form": "#form-validate",
"regionJson": <?= /* @noEscape */ $this->helper(\Magento\Directory\Helper\Data::class)
->getRegionJson() ?>,
"regionJson": <?= /* @noEscape */ $viewModel->dataGetRegionJson() ?>,
"currentRegionValue": "<?= /* @noEscape */ $formData->getRegion() ?>",
"defaultRegion": "<?= (int) $formData->getRegionId() ?>",
"countriesWithOptionalZip": <?= /* @noEscape */ $this->helper(\Magento\Directory\Helper\Data::class)
->getCountriesWithOptionalZip(true) ?>
"countriesWithOptionalZip": <?= /* @noEscape */ $viewModel->dataGetCountriesWithOptionalZip(true) ?>
}
}
}
Expand Down
Loading