0

I have problem with postcode field in magento 2. Somewhere magento remove class required from

<div class="field zip required test">
        <label class="label" for="zip"><span><?php /* @escapeNotVerified */ echo __('Zip/Postal Code') ?></span></label>
        <div class="control">
            <input type="text" name="postcode" value="<?php echo $block->escapeHtml($block->getAddress()->getPostcode()) ?>" title="<?php /* @escapeNotVerified */ echo __('Zip/Postal Code') ?>" id="zip" class="input-text validate-zip-international <?php /* @escapeNotVerified */ echo $this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('postcode') ?>">
        </div>
    </div>

When i added class 'test' i see this, but required is removed somewhere. Do you have any hint? In database I update eav_attribute postcode (I set is_required= 1)

edit: I find solution. Problem was in getCountriesWithOptionalZip(). Now zip code is required, but not validate postcode by country. Any hints?

0

If you want to set postcode field required then you have to add data-validate="{required:true}" in input .

Your final html will be

<input type="text" data-validate="{required:true}" name="postcode" value="<?php echo $block->escapeHtml($block->getAddress()->getPostcode()) ?>" title="<?php /* @escapeNotVerified */ echo __('Zip/Postal Code') ?>" id="zip" class="input-text validate-zip-international <?php /* @escapeNotVerified */ echo $this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('postcode') ?>">

Magento 2 does not validate input field the way it used to do in Magento 1 by adding class.

  • I added data-validate, but still magento remove class from div. Additional not work validate zip code by country. – Mjuzik Jan 30 '18 at 11:07
  • just add data-validate you don't need to add class – Piyush Jan 30 '18 at 11:07
  • check this answer magento.stackexchange.com/a/149857/20064 – Piyush Jan 30 '18 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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