0

I have tried to change the address via account dashboard but its not working and I'm getting the below error:

1 exception(s):
Exception #0 (Exception): Notice: Undefined index: region in /var/www/html/butterflynov22/butterfly/vendor/magento/module-customer/Block/Address/Edit.php on line 122

So I have changed to the default luma theme and I can edit the address without issue.

Edit.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
/**
 * Edit customer address template
 *
 * @var $block \Magento\Customer\Block\Address\Edit
 */
/** @var \Magento\Customer\Helper\Address $addressHelper */
$addressHelper = $this->helper('Magento\Customer\Helper\Address');
$requiredAttributesPrechecked = $block->getData('required_attributes_prechecked');
?>
<form class="form-address-edit address-edit-wrap" action="<?= /* @escapeNotVerified */ $block->getSaveUrl() ?>" method="post" id="form-validate" enctype="multipart/form-data" data-hasrequired="<?= /* @escapeNotVerified */ __('* Required Fields') ?>">
    <fieldset class="fieldset myacc-fieldset-edit">
        <legend class="legend"><span><?= /* @escapeNotVerified */ __('Contact Information') ?></span></legend><br>
        <?= $block->getBlockHtml('formkey') ?>
        <input type="hidden" name="success_url" value="<?= /* @escapeNotVerified */ $block->getSuccessUrl() ?>"/>
        <input type="hidden" name="error_url" value="<?= /* @escapeNotVerified */ $block->getErrorUrl() ?>"/>
        <?= $block->getNameBlockHtml() ?>
        <div class="address-row-wrap">
            <div class="col-6">
        <?php if ($addressHelper->isAttributeVisible('company')): ?>
        <div class="field company single-row <?php if (in_array('company', $requiredAttributesPrechecked)): ?>required<?php endif; ?>">
            <label class="label" for="company"><span><?= /* @escapeNotVerified */ __('Company') ?></span></label>
            <div class="control">
                <input type="text" name="company" id="company" title="<?= /* @escapeNotVerified */ __('Company') ?>" value="<?= $block->escapeHtml($block->getAddress()->getCompany()) ?>" class="input-text my-account-input <?= /* @escapeNotVerified */ $addressHelper->getAttributeValidationClass('company') ?>"/>
            </div>
        </div>
        <?php endif; ?>
        </div>
        <div class="col-6">
        <?php if ($addressHelper->isAttributeVisible('telephone')): ?>
            <div class="field telephone single-row <?php if (in_array('telephone', $requiredAttributesPrechecked)): ?>required<?php endif; ?>">
            <label class="label" for="telephone"><span><?= /* @escapeNotVerified */ __('Phone Number') ?></span></label>
            <div class="control">
                <input type="text" name="telephone" value="<?= $block->escapeHtml($block->getAddress()->getTelephone()) ?>" title="<?= /* @escapeNotVerified */ __('Phone Number') ?>" class="input-text my-account-input
                 <?= /* @escapeNotVerified */ $addressHelper->getAttributeValidationClass('telephone') ?>" id="telephone"/>
            </div>
        </div>
        <?php endif; ?>
        </div>
    </div>
        <?php if ($addressHelper->isAttributeVisible('fax')): ?>
        <div class="field fax">
            <label class="label" for="fax"><span><?= /* @escapeNotVerified */ __('Fax') ?></span></label>
            <div class="control">
                <input type="text" name="fax" id="fax" title="<?= /* @escapeNotVerified */ __('Fax') ?>" value="<?= $block->escapeHtml($block->getAddress()->getFax()) ?>" class="input-text my-account-input<?= /* @escapeNotVerified */ $addressHelper->getAttributeValidationClass('fax') ?>"/>
            </div>
        </div>
        <?php endif; ?>
    </fieldset>
    <fieldset class="fieldset myacc-fieldset-edit">
        <legend class="legend"><span><?= /* @escapeNotVerified */ __('Address') ?></span></legend><br>
        <?php $_streetValidationClass = $addressHelper->getAttributeValidationClass('street'); ?>
        <div class="field street required">
            <label for="street_1" class="label"><span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('street') ?></span></label>
            <div class="control">
                <input type="text" name="street[]" value="<?= $block->escapeHtml($block->getStreetLine(1)) ?>" title="<?= /* @escapeNotVerified */ __('Street Address') ?>" id="street_1" class="input-text my-account-input
                 <?= /* @escapeNotVerified */ $_streetValidationClass ?>"/>
                <div class="nested">
                    <?php $_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass)); ?>
                    <?php for ($_i = 2, $_n = $addressHelper->getStreetLines(); $_i <= $_n; $_i++): ?>
                        <div class="field additional">
                            <label class="label" for="street_<?= /* @escapeNotVerified */ $_i ?>">
                                <span><?= /* @escapeNotVerified */ __('Street Address %1', $_i) ?></span>
                            </label>
                            <div class="control">
                                <input type="text" name="street[]" value="<?= $block->escapeHtml($block->getStreetLine($_i)) ?>" title="<?= /* @escapeNotVerified */ __('Street Address %1', $_i) ?>" id="street_<?= /* @escapeNotVerified */ $_i ?>" class="input-text my-account-input<?= /* @escapeNotVerified */ $_streetValidationClass ?>"/>
                            </div>
                        </div>
                    <?php endfor; ?>
                </div>
            </div>
        </div>

        <?php if ($addressHelper->isVatAttributeVisible()) : ?>
            <div class="field taxvat">
                <label class="label" for="vat_id"><span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('vat_id') ?></span></label>
                <div class="control">
                    <input type="text" name="vat_id" value="<?= $block->escapeHtml($block->getAddress()->getVatId()) ?>" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('vat_id') ?>" class="input-text my-account-input<?= /* @escapeNotVerified */ $this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('vat_id') ?>" id="vat_id"/>
                </div>
            </div>
        <?php endif; ?>
        <div class="address-row-wrap">
            <div class="col-6">
                <div class="field city required">
                    <label class="label" for="city"><span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('city') ?></span></label>
                    <div class="control">
                        <input type="text" name="city" value="<?= $block->escapeHtml($block->getAddress()->getCity()) ?>" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('city') ?>" class="input-text my-account-input <?= /* @escapeNotVerified */ $this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('city') ?>" id="city"/>
                    </div>
                </div>
            </div>

            <div class="col-6">
                <div class="field region required">
                    <label class="label" for="region_id"><span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?></span></label>
                    <div class="control">
                        <select id="region_id" name="region_id" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>" class="validate-select region_id my-account-input" <?= (!$block->getConfig('general/region/display_all')) ? ' disabled="disabled"' : '';?>/>
                            <option value=""><?= /* @escapeNotVerified */ __('Please select a region, state or province.') ?></option>
                        </select>
                        <input type="text" id="region" name="region" value="<?= $block->escapeHtml($block->getRegion()) ?>"  title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>" class="input-text my-account-input<?= /* @escapeNotVerified */ $this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('region') ?>"<?= (!$block->getConfig('general/region/display_all')) ? ' disabled="disabled"' : '';?>/>
                    </div>
                </div>
            </div>
        </div>
        <div class="address-row-wrap">
            <div class="col-6">
                <div class="field zip required">
                    <label class="label" for="zip"><span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('postcode') ?></span></label>
                    <div class="control">
                        <input type="text" name="postcode" value="<?= $block->escapeHtml($block->getAddress()->getPostcode()) ?>" title="<?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('postcode') ?>" id="zip" class="input-text validate-zip-international my-account-input<?= /* @escapeNotVerified */ $this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('postcode') ?>"/>
                        <div role="alert" class="message warning" style="display:none">
                            <span></span>
                        </div>
                    </div>
                </div>
           </div>
           <div class="col-6">
                <div class="field country required">
                    <label class="label" for="country"><span><?= /* @noEscape */ $block->getAttributeData()->getFrontendLabel('country_id') ?></span></label>
                    <div class="control">
                        <?= $block->getCountryHtmlSelect() ?>
                    </div>
                </div>
           </div>
        </div>

        <?php $userDefinedAttributes = $block->getLayout()->getBlock('customer_form_address_user_attributes'); ?>
        <?php if ($userDefinedAttributes):?>
            <?= $userDefinedAttributes->setShowContainer(false)->toHtml() ?>
        <?php endif;?>
        <?php if ($block->isDefaultBilling()): ?>
            <div class="message info">
                <span><?= /* @escapeNotVerified */ __("This is your default billing address.") ?></span>
            </div>
        <?php elseif ($block->canSetAsDefaultBilling()): ?>
            <div class="field choice set billing">
                <input type="checkbox" id="primary_billing" name="default_billing" value="1" class="checkbox"/>
                <label class="label" for="primary_billing"><span><?= /* @escapeNotVerified */ __('Use as my default billing address') ?></span></label>
            </div>
        <?php else: ?>
            <input type="hidden" name="default_billing" value="1"/>
        <?php endif; ?>

        <?php if ($block->isDefaultShipping()): ?>
            <div class="message info">
                <span><?= /* @escapeNotVerified */ __("This is your default shipping address.") ?></span>
            </div>
        <?php elseif ($block->canSetAsDefaultShipping()): ?>
            <div class="field choice set shipping">
                <input type="checkbox" id="primary_shipping" name="default_shipping" value="1" class="checkbox"/>
                <label class="label" for="primary_shipping"><span><?= /* @escapeNotVerified */ __('Use as my default shipping address') ?></span></label>
            </div>
        <?php else: ?>
            <input type="hidden" name="default_shipping" value="1"/>
        <?php endif; ?>
    </fieldset>
    <div class="actions-toolbar">
        <div class="primary">
            <button type="submit" class="action submit primary" data-action="save-address" title="<?= /* @escapeNotVerified */ __('Save Address') ?>">
                <span><?= /* @escapeNotVerified */ __('Save Address') ?></span>
            </button>
        </div>
        <div class="secondary">
            <a class="action back" href="<?= $block->escapeUrl($block->getBackUrl()) ?>">
                <span><?= /* @escapeNotVerified */ __('Go back') ?></span>
            </a>
        </div>
    </div>
</form>
<script type="text/x-magento-init">
    {
        "#form-validate": {
            "addressValidation": {
                "postCodes": <?= /* @noEscape */ $block->getPostCodeConfig()->getSerializedPostCodes(); ?>
            }
        },
        "#country": {
            "regionUpdater": {
                "optionalRegionAllowed": <?= /* @escapeNotVerified */ ($block->getConfig('general/region/display_all') ? 'true' : 'false') ?>,
                "regionListId": "#region_id",
                "regionInputId": "#region",
                "postcodeId": "#zip",
                "form": "#form-validate",
                "regionJson": <?= /* @escapeNotVerified */ $this->helper('Magento\Directory\Helper\Data')->getRegionJson() ?>,
                "defaultRegion": "<?= /* @escapeNotVerified */ $block->getRegionId() ?>",
                "countriesWithOptionalZip": <?= /* @escapeNotVerified */ $this->helper('Magento\Directory\Helper\Data')->getCountriesWithOptionalZip(true) ?>
            }
        }
    }
</script>

But If I change the theme to our extended luma theme, its not working and It shows the above mentioned error.

Can anyone please help me to rectify this issue?

6
  • Can you pls upload your theme's address/edit.phtml file? Commented Nov 26, 2019 at 12:04
  • @RohanHapani, I have included the edit.phtml file in question section.
    – MagentoDev
    Commented Nov 26, 2019 at 12:57
  • We just copied the edit.phtl from core and pasted it on our theme with our custom css class names. We haven't changed anything in edit.phtml
    – MagentoDev
    Commented Nov 26, 2019 at 13:02
  • No. I said about app/design/vendor/module/Magento_Customer/templates/address/edit.phtml file Commented Nov 27, 2019 at 4:20
  • @RohanHapani We haven't have the edit.phtml in the path as you said.
    – MagentoDev
    Commented Nov 27, 2019 at 5:40

1 Answer 1

1

When InputException is thrown in \Magento\Customer\Controller\Address\FormPost::execute then PostValue sets in AddressFormData and redirect performs to \Magento\Customer\Block\Address\Edit. As a result error occurs because in array AddressFormData doesn't exist yet this element('region').

Solution: create the after plugin

<?php 

namespace Vendor\Module\Plugin\Controller\Address\FormPost;

use Magento\Framework\Controller\Result\Redirect;
use Magento\Customer\Controller\Address\FormPost;

class FixAddressFormData extends FormPost
{
    public function afterExecute(FormPost $subject, Redirect $result): Redirect
{
    $addressFormData = $subject->_getSession()->getAddressFormData();
    $addressFormData['region'] = isset($addressFormData['region']) ? $addressFormData['region_id'] : null;
    $subject->_getSession()->setAddressFormData($addressFormData);
    return $result;
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.