magento2: M 2.4.0 Contact Form has error and can't be rendered

Preconditions (*)

  1. Debian 10, Apache 2.4, PHP 7.3.19, mysql Ver 15.1 Distrib 10.3.22-MariaDB Initially using Magento 2.3, with Magento Blank theme and no sample data or customized code. Had the start of a store, with basic data. The “Contact Us” footer link was basic, but worked fine.

  2. After upgrade from Magento 2.3.5-p1 to Magento 2.4.0, the form does not render and generates an error.

Steps to reproduce (*)

  1. Using vanilla Magento 2.3.5-p1, upgraded to 2.4.0. NO customizations, almost no data, just basic store info, contact info, and the following in the “Contact Us” CONTENT-> Pages page:
<div style="float: left; padding-right: 15px;">
<p>Need some help? We'd love to hear from you!</p>
<p>Call us: 503-555-1212</p>
<p><img src="{{media url="woman-typing.jpg"}}" alt="" width="297" height="198">;</p>
</div>
<p>{{block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form.phtml"}}</p>
  1. View front-end “Customer View”,
  2. click on the “Contact Us” footer link

Expected result (*)

  1. I expect to see the basic blank theme Contact Us form

Actual result (*)

  1. The page renders like this: Screenshot from 2020-07-30 11-11-02

The error is as follows:

<input name="name" id="name" title="Name" value="Error: Call to a member function getUserName() on null in /var/www/html/magento/vendor/magento/module-contact/view/frontend/templates/form.phtml:29
Stack trace:
#0 /var/www/html/magento/vendor/magento/framework/View/TemplateEngine/Php.php(71): include()
#1 /var/www/html/magento/vendor/magento/framework/View/Element/Template.php(273): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Contact\Block\ContactForm), '/var/www/html/m...', Array)
#2 /var/www/html/magento/vendor/magento/framework/View/Element/Template.php(303): Magento\Framework\View\Element\Template->fetchView('/var/www/html/m...')
#3 /var/www/html/magento/vendor/magento/framework/View/Element/AbstractBlock.php(1111): Magento\Framework\View\Element\Template->_toHtml()
#4 /var/www/html/magento/vendor/magento/framework/View/Element/AbstractBlock.php(1115): Magento\Framework\View\Element\AbstractBlock->Magento\Framework\View\Element\{closure}()
#5 /var/www/html/magento/vendor/magento/framework/View/Element/AbstractBlock.php(675): Magento\Framework\View\Element\AbstractBlock->_loadCache()
#6 /var/www/html/magento/vendor/magento/module-email/Model/Template/Filter.php(426): Magento\Framework\View\Element\AbstractBlock->toHtml()
#7 [internal function]: Magento\Email\Model\Template\Filter->blockDirective(Array)
#8 /var/www/html/magento/vendor/magento/framework/Filter/DirectiveProcessor/LegacyDirective.php(42): ReflectionMethod->invokeArgs(Object(Magento\Widget\Model\Template\Filter), Array)
#9 /var/www/html/magento/vendor/magento/framework/Filter/Template.php(184): Magento\Framework\Filter\DirectiveProcessor\LegacyDirective->process(Array, Object(Magento\Widget\Model\Template\Filter), Array)
#10 /var/www/html/magento/vendor/magento/module-email/Model/Template/Filter.php(1080): Magento\Framework\Filter\Template->filter('<div style=" flo...')="" #11="" var="" www="" html="" magento="" vendor="" module-cms="" block="" page.php(170):="" magento\email\model\template\filter-="">

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 41 (19 by maintainers)

Most upvoted comments

Hi @bluedarter2 ,

We have tried to reproduce this issue as per the mentioned steps, but we are unable to do the same. We have followed below URL to upgrade the Magento from 2.3.5-p1 to 2.4.0:

https://devdocs.magento.com/guides/v2.3/comp-mgr/cli/cli-upgrade.html

Before Upgrade - 2.3.5-p1 Admin dashboard with version information: VersionCheckBFUpgrade

Contact us page before upgrade: ContactUsPageBFUpgrade

After Upgrade - 2.4.0 Admin dashboard with version information: VersionCheckAFUpgrade

Contact us page after upgrade: ContactUsPageAFUpgrade

Please check once at your end and let us know if you still facing any issue related with that.

If you call form via CMS page your template will missing view_model argument. That’s reason your page will break. Some old theme still missing view_model and not update following Magento Implement. Another option is overwrite entire block contact by your own file (not recommended). You have to manually update your file with magento

I checked and we don’t have the XML customizations available in the CMS pages anymore, so we can’t fix it via admin panel. The page that contains the contact form must implement the view_model via XML.

    <referenceContainer name="content">
            <block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form.phtml">
                <container name="form.additional.info" label="Form Additional Info"/>
                <arguments>
                    <argument name="view_model" xsi:type="object">Magento\Contact\ViewModel\UserDataProvider</argument>
                </arguments>
            </block>
    </referenceContainer>

You can follow this

I checked and we don’t have the XML customizations available in the CMS pages anymore, so we can’t fix it via admin panel. The page that contains the contact form must implement the view_model via XML.

    <referenceContainer name="content">
            <block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form.phtml">
                <container name="form.additional.info" label="Form Additional Info"/>
                <arguments>
                    <argument name="view_model" xsi:type="object">Magento\Contact\ViewModel\UserDataProvider</argument>
                </arguments>
            </block>
    </referenceContainer>

@hostep Thanks. I disabled that page, and now the Contact Us link goes to the 404 page not found. To be honest, I started stepping through the Magento store setup just after the first of the year (Jan 2020) then had to stop working on it. I’ve just picked it up again, and I can not for the life of me remember how to affect what happens when that link in the footer is clicked. I find Magento to be pretty difficult to understand/configure for a newbie. The shop I need to replace is a pre-Joomla (Mambo) php_shop (pre-VirtueMart).

[edit to add] OK, I “fixed” it by removing the “Contact Us” page that I had created.