Magento2: Checkout: ZIP code in shipping address form already validated on page load

Created on 8 Dec 2016  路  13Comments  路  Source: magento/magento2

When loading the checkout page and no shipping address has been saved yet (for example during guest checkout), the ZIP code field will already show as having failed the validation ("this is a required field") without any user interaction.

Preconditions

  1. Magento CE 2.1.1

Steps to reproduce

  1. make sure no user is logged in
  2. put articles into cart
  3. proceed to checkout

Expected result

  1. Once the checkout page finishes loading, all text fields of the shipping address form should be empty and not yet validated.

Actual result

  1. All fields are empty, but the ZIP code field is already validated and shows a red frame and "this is a required field" message.
    screenshot-qp2-local dev refusion com 2016-12-08 11-08-28

Investigation

The problem seems to be that the field value is programmatically set before the page has finished loading. We haven't found out what causes this to happen. However, the problem is that the new value for the field is undefined. Then previous value is an empty string. The equalityComparer for the ZIP code field is the valuesArePrimitiveAndEqual(a, b) method (/lib/web/knockoutjs/knockout.js). So the method isDifferent: function(oldValue, newValue) (/lib/web/knockoutjs/knockout.js) returns true.

Workaround

For all practical purposes we are fine with the empty string being considered equal to null or undefined, so we can replace the equalityComparer property of the ZIP code field. We can do this by overriding the initObservable() method in /vendor/magento/module-ui/view/base/web/js/form/element/post-code.js. See attached patch.
avoid-premature-zip-code-validation-vendor_magento_module-ui_view_base_web_js_form_element_post-code.diff.zip

Checkout Fixed in 2.2.x Format is valid needs update bug report

Most helpful comment

The problem also exist in the newest Magento 2.2.5.

All 13 comments

Thanks. Code is working.

Thanks for the fix

Hopefully this will be added to 2.1.4 or 2.2.

issue is still present in 2.1.4

thanks for the fix, you saved me a lot

that works perfect thanks it really saves a lot of time .

Would it not be easier fto provide in a switch at store level in the configuration for the Postcode prepopulation of "*", because it is causing all sorts of failed transactions for us and no doubt other webstore devs.

Many thanks,

Guy

Thanks you very very much. Code working ! @ktruehl

@ktruehl May please let me know if this issue relevant for latest release of Magento 2?

The problem also exist in the newest Magento 2.2.5.

Issue still exists in 2.2.6

Can also reproduce..

Still exists in 2.3.0. Just have to set a postcode in "Default Tax Destination Calculation"

Which is set here:
http://Base_url/admin/admin/system_config/edit/section/tax/
postcode set here

Use Xdebug in any active shipping module on calling RateRequest->getDestPostcode(). We observe that:

Magento\Quote\Model\Quote\Address\RateRequest->$_data['dest_postcode'] = Default Tax Destination Calculation
Was this page helpful?
0 / 5 - 0 ratings