magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Coupling beetwen Magento_Checkout::js/view/shipping.js:validateShippingInformation() and layout definition or view.

Open funkyproject opened this issue 7 years ago • 6 comments

Summary

On checkout shipping step, I reorganized the shipping address form and moved the postCode and countryId fields into a group.

I can't validate the shipping address without overriding the Magento_Checkout:js/view/shipping.js file and particulary the validateShippingInformation().

country = registry.get(this.parentName + '.shippingAddress.shipping-address-fieldset.country_id'),

In my case, I need to change this to :

country = registry.get(this.parentName + '.shippingAddress.shipping-address-fieldset.address_group.country_id'),

Preconditions

  1. Magento CE 2.3.1

Steps to reproduce

  1. Move the country_id field of shipping form into checkout.
  2. validate the shipping address.

Examples (*)

This is the layoutProcessorPlugin for reorganize the shipping and billing form:

use Magento\Checkout\Block\Checkout\LayoutProcessor;

/**
 * Alter  Layout Processor and change shipping form
 */
class LayoutProcessorPlugin
{
    /**
     * Change jsLayout after process.
     *
     * @param LayoutProcessor $subject
     * @param array $jsLayout
     *
     * @return array
     *
     * @SuppressWarnings("unused")
     */
    public function afterProcess(LayoutProcessor $subject, $jsLayout): array
    {
        $this->updateShippingForm($jsLayout);
        $this->updateBillingForm($jsLayout);
        $this->moveBillingForm($jsLayout);

        return $jsLayout;
    }

    /**
     * Update shipping form.
     *
     * @param array $jsLayout
     */
    private function updateShippingForm(&$jsLayout): void
    {
        $fieldSet = &$jsLayout['components']['checkout']['children']
        ['steps']['children']['shipping-step']['children']['shippingAddress']
        ['children']['shipping-address-fieldset']['children'];

        $this->updateAddressForm($fieldSet, true);
    }

    /**
     * Update billing form.
     *
     * @param array $jsLayout
     */
    private function updateBillingForm(&$jsLayout): void
    {
        $fieldSet = &$jsLayout['components']['checkout']['children']
        ['steps']['children']['billing-step']['children']['payment']['children']['afterMethods']['children']
        ['billing-address-form']['children']['form-fields']['children'];

        $this->updateAddressForm($fieldSet, false);
    }

    /**
     * Update address form.
     *
     * @param array $fieldSet
     * @param bool $isShipping
     *
     * @SuppressWarnings(PHPMD)
     */
    private function updateAddressForm(&$fieldSet, bool $isShipping): void
    {
        $sort = [
            'prefix'             => ['sortOrder' => 5],
            'firstname_lastname' => ['sortOrder' => 10],
            'company_group'      => ['sortOrder' => 20],
            'address_group'      => ['sortOrder' => 200],
            'last_group'         => ['sortOrder' => 220],
            'region_id'          => ['sortOrder' => 210]
        ];

        $fieldSet['email'] = $fieldSet['firstname'];
        $fieldSet['email']['type'] = 'email';
        $fieldSet['email']['label'] = __('Email');
        $fieldSet['email']['dataScope'] = 'shippingAddress.email';

        $fieldSet['prefix']['component'] = 'Magento_Ui/js/form/element/checkbox-set';
        $fieldSet['prefix']['multiple'] = false;
        $fieldSet['prefix']['config']['template'] = 'ui/form/element/checkbox-set';
        $fieldSet['prefix']['config']['elementTpl'] = 'ui/form/components/single/radio';

        $fieldSet['firstname']['sortOrder'] = 2;
        $fieldSet['lastname']['sortOrder'] = 1;

        $fieldSet['postcode']['validation']['domtom'] = true;

        unset($fieldSet['telephone']['config']['tooltip']);
        $groups = [];
        $groups['firstname_lastname'] = [
            'component' => 'Magento_Ui/js/form/components/group',
            'label'     => [],
            'dataScope' => '',
            'provider'  => 'checkoutProvider',
            'sortOrder' => 1,
            'type'      => 'group',
            'config'    => [
                'template'          => 'ui/group/group',
                'additionalClasses' => 'form__row form__row--two'
            ],
            '_children' => ['lastname', 'firstname']
        ];

        $groups['company_group'] = [
            'component' => 'Magento_Ui/js/form/components/group',
            'label'     => [],
            'dataScope' => '',
            'provider'  => 'checkoutProvider',
            'sortOrder' => 2,
            'type'      => 'group',
            'config'    => [
                'template'          => 'ui/group/group',
                'additionalClasses' => 'form__row form__row--two'
            ],
            '_children' => ['company']
        ];

        $groups['address_group'] = [
            'component' => 'Magento_Ui/js/form/components/group',
            'label'     => [],
            'dataScope' => '',
            'provider'  => 'checkoutProvider',
            'sortOrder' => 2,
            'type'      => 'group',
            'config'    => [
                'template'          => 'ui/group/group',
                'additionalClasses' => 'form__row form__row--three'
            ],
            '_children' => ['postcode', 'city', 'country_id']
        ];

        $groups['last_group'] = [
            'component' => 'Magento_Ui/js/form/components/group',
            'label'     => [],
            'dataScope' => '',
            'provider'  => 'checkoutProvider',
            'sortOrder' => 2,
            'type'      => 'group',
            'config'    => [
                'template'          => 'ui/group/group',
                'additionalClasses' => 'form__row form__row--three'
            ],
            '_children' => ['telephone', 'email']
        ];

        $fieldSet['postcode']['config']['additionalClasses'] = 'field--15';
        $fieldSet['postcode']['sortOrder'] = 1;
        $fieldSet['city']['config']['additionalClasses'] = 'field--30';
        $fieldSet['city']['sortOrder'] = 2;
        $fieldSet['country_id']['config']['additionalClasses'] = 'field--48';
        $fieldSet['country_id']['sortOrder'] = 3;
        $fieldSet['email']['sortOrder'] = 2;
        $fieldSet['telephone']['sortOrder'] = 1;

        $fieldSet['street']['children'][0]['label'] = __('Address');
        $fieldSet['street']['children'][1]['label'] = __('Additional address');
        unset($fieldSet['street']['children'][2]);

        foreach ($groups as $name => $group) {
            $group['children'] = [];
            foreach ($group['_children'] as $childrenName) {
                $group['children'][$childrenName] = $fieldSet[$childrenName];
                unset($fieldSet[$childrenName]);
            }
            $fieldSet[$name] = $group;
        }

        foreach ($sort as $item => $config) {
            $fieldSet[$item] = array_merge($fieldSet[$item], $config);
        }

        unset($fieldSet['region_id']);
    }

    /**
     * Move billing form.
     *
     * @param array $jsLayout
     */
    private function moveBillingForm(&$jsLayout): void
    {
        $billingStep = &$jsLayout['components']['checkout']['children']['steps']['children']
        ['billing-step']['children']['payment']['children'];

        $afterMethods = $billingStep['afterMethods']['children'];
        $billingStep['afterMethods']['children'] = [];

        unset($afterMethods['discount']);
        $billingStep['beforeMethods']['children'] = array_merge(
            $billingStep['beforeMethods']['children'],
            $afterMethods
        );
    }
}

Proposed solution

No idea, but if we could set an alias for the uiRegistry, We could decoupling the view and validation logic.

funkyproject avatar Apr 18 '19 15:04 funkyproject

Hi @funkyproject. Thank you for your report. To help us process this issue please make sure that you provided the following information:

  • [ ] Summary of the issue
  • [ ] Information on your environment
  • [ ] Steps to reproduce
  • [ ] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento-engcom-team give me 2.3-develop instance - upcoming 2.3.x release

For more details, please, review the Magento Contributor Assistant documentation.

@funkyproject do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [ ] yes
  • [ ] no

m2-assistant[bot] avatar Apr 18 '19 15:04 m2-assistant[bot]

Hi @engcom-backlog-nazar. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • [ ] 5. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • [ ] 6. Add label Issue: Confirmed once verification is complete.

  • [ ] 7. Make sure that automatic system confirms that report has been added to the backlog.

m2-assistant[bot] avatar Apr 26 '19 11:04 m2-assistant[bot]

Hey @funkyproject, thanks for finding this out!

Indeed, having paths to different components hardcoded in the source is not in line with Magento best practices. I took some time and applies some changes in https://github.com/magento/magento2/pull/25541/files that I found the most sensible. With it, you should be able to pass adjusted paths to country components through configuration which you seem to be customizing anyway.

Waiting for your feedback regarding this proposed solution.

krzksz avatar Nov 09 '19 12:11 krzksz

:white_check_mark: Confirmed by @krzksz Thank you for verifying the issue! :+1: Your confirmation will help us to acknowledge and process this report.

ghost avatar Nov 09 '19 12:11 ghost

Hi @krzksz. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [ ] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [ ] 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • [ ] 5. Add label Issue: Confirmed once verification is complete.

  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

m2-assistant[bot] avatar Nov 21 '19 20:11 m2-assistant[bot]

Hi @funkyproject. Thank you for your report. The issue has been fixed in magento/magento2#25541 by @krzksz in 2.4-develop branch Related commit(s):

The fix will be available with the upcoming 2.4.0 release.

VladimirZaets avatar Dec 08 '19 17:12 VladimirZaets