3

I have to add captcha on the contact form. It's working on the default contact URL but not working with the contact form on CMS page.

<p>{{block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form.phtml"}}</p>

<referenceContainer name="form.additional.info">
<block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false">
    <action method="setFormId">
        <argument name="formId" xsi:type="string">contact_us</argument>
    </action>
    <action method="setImgWidth">
        <argument name="width" xsi:type="string">230</argument>
    </action>
    <action method="setImgHeight">
        <argument name="width" xsi:type="string">50</argument>
    </action>
</block>
</referenceContainer>
<referenceContainer name="after.body.start">
<referenceBlock name="head.components">
    <block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"/>
</referenceBlock>
</referenceContainer>

Please give me some suggestion or help.

  • Paste the code you have added on your cms page? – Sukumar Gorai Jul 5 '18 at 8:00
  • @magecode, please try my answer. – Mohit Kumar Arora Jul 5 '18 at 11:57
4

There is no need to call <p>{{block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form.phtml"}}</p> in the content tab.

If you don't have any other content to show in the content tab, then you can use &nbsp; in the Content tab.

Steps to show captcha in contact form in CMS page are listed below:

Step 1: Open the CMS page in which you want to show the contact form with captcha and click on the Design tab.

Step 2: Paste the following code in the Layout Update XML textarea:

<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">
            <block class="Magento\Captcha\Block\Captcha" name="captcha" after="-" cacheable="false">
                <action method="setFormId">
                    <argument name="formId" xsi:type="string">contact_us</argument>
                </action>
                <action method="setImgWidth">
                    <argument name="width" xsi:type="string">230</argument>
                </action>
                <action method="setImgHeight">
                    <argument name="width" xsi:type="string">50</argument>
                </action>
            </block>
        </container>
    </block>
</referenceContainer>

<referenceContainer name="after.body.start">
<referenceBlock name="head.components">
    <block class="Magento\Framework\View\Element\Js\Components" name="captcha_page_head_components" template="Magento_Captcha::js/components.phtml"/>
</referenceBlock>
</referenceContainer>

Step 3: Save the page.

Note: After form submission, the page will redirect to default contact page. If you want to change it then override the controller.

  • Yes it's working but now I am getting error with overridden controller Call to a member function send() on null With line $this->sendEmail($this->validatedParams()); – Magecode Jul 5 '18 at 12:40
  • I am getting data in this $post array private function sendEmail($post) { print_r($post); $this->mail->send($post['email'], ['data' => new \Magento\Framework\DataObject($post)]); } – Magecode Jul 5 '18 at 12:41
  • If my answer to the current question is working, please upvote my answer and ask this question as a different question in detail. From the comment it is hard to understand the details. – Mohit Kumar Arora Jul 5 '18 at 12:44
  • I have posted new question please check – Magecode Jul 5 '18 at 12:57
  • @Magecode, please post URL here so that I can find it easily. – Mohit Kumar Arora Jul 5 '18 at 12:57

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.