How to make phone number a mandatory field in contact page?

If you want to make the phone number a mandatory field in the contact page you will need to override the form.phtml template file.

Copy the form.phtml template file used for your current contact form to the folder Magento_Contact/templates inside your theme’s folder. If your current theme does not have that template file it is inheriting from Magento luma or blank themes. Then copy the template file from vendor/magento/module-contact/view/frontend/templates/form.phtml.

Now, edit the div responsible for rendering to the phone field in the form and change it to:

        <div class="field telephone required">
            <label class="label" for="telephone"><span><?= $block->escapeHtml(__('Phone Number')) ?></span></label>
            <div class="control">
                <input name="telephone" id="telephone" title="<?= $block->escapeHtmlAttr(__('Phone Number')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('telephone')) ?>" class="input-text" type="text" data-validate="{required:true}"/>
            </div>
        </div>

Note the additional class required in line 30 (to show off the required *) and the added data-validate attribute with value {required: true} to tell magento that this is now a required input for this form.

Clear the cache and you are good to go 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *