0

I know there are questions here that are about how to make the address required during checkout. This is not that question.

My question is how I can make all address fields required in the customer account page. When the customer is logged in and want to add a new address or edit an existing one. I thought it would be simple: just add the required-entry-class. But it turns out that this doesn't work:

<?php for ($_i = 1, $_n = $this->helper(\Magento\Customer\Helper\Address::class)->getStreetLines(); $_i < $_n; $_i++): ?>
    <div class="field additional required">
        <label class="label" for="street_<?php /* @escapeNotVerified */ echo $_i+1 ?>">
            <span><?php echo $block->escapeHtml(__('Street ' . $_i)) ?></span>
        </label>
        <div class="control">
            <input placeholder="<?php echo $block->escapeHtml(__('Street ' . $_i)) ?>"
                   type="text"
                   name="street[]"
                   value="<?php echo $this->helper(\Magento\Framework\EscapeHelper::class)->escapeHtmlAttr($block->getStreetLine($_i+1)) ?>"
                   title="<?php  echo $this->helper(\Magento\Framework\EscapeHelper::class)->escapeHtmlAttr(__('Street Address %1', $_i + 1)) ?>"
                   id="street_<?php /* @escapeNotVerified */ echo $_i+1 ?>"
                   class="input-text required-entry">
        </div>
    </div>
<?php endfor; ?>

So why doesn't this work? And how can I get it to work?

1

For those encountering this same issue. The reason behind this is that Magento 2's validator only validates the first input element based by name. In this case, all names are street[], so only the first one is validated. This is also explained in this question.

The solution in this case is to edit the form to make sure the names of the field reflect street[0], street[1], street[2], etc ...

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.