4

I have created a cms page named "custom" ad this page has content {{block type="core/template" template="my/custom.phtml"}}. And in custom.phtml created a custom form with custom validation and custom controller action for submission.

Now I want to add captcha to this form.

For this I have added below xml script to cms xml field :

<reference name="content">
    <!-- Code Start For Display Captcha -->
    <block type="captcha/captcha" name="captcha">
            <reference name="head">
                <action method="addJs"><file>mage/captcha.js</file></action>
            </reference>
            <action method="setFormId"><formId>form-validate</formId></action>
            <action method="setImgWidth"><width>230</width></action>
            <action method="setImgHeight"><width>50</width></action>
    </block> 
    <!-- Code End For Display Captcha -->


</reference>

And <?php echo $this->getChildHtml('captcha'); ?> on custom.phtml file.

But captcha not appearing on page.

How to resolve it?

1

1 Answer 1

5

Have a look at this answer

Magento: Add captcha to custom contact form

NameSpace : MyPackage and Module: MyModule

Module configuration

location : app/etc/modules/MyPackage_MyModule.xml

<config>
<modules>
    <MyPackage_MyModule>
        <active>true</active>
        <codePool>local</codePool>
    </MyPackage_MyModule>
</modules>

Create config file for this module

location : app/code/local/MyPackage/MyModule/etc/config.xml

<config>
<modules>
    <MyPackage_MyModule>
        <version>0.0.0.1</version>
    </MyPackage_MyModule>
</modules>
<global>
    <models>
        <mymodule>
            <class>MyPackage_MyModule_Model</class>
        </mymodule>
    </models>
    <events>
        <controller_action_predispatch_contacts_index_post>
            <observers>
                <mymodule>
                    <class>mymodule/observer</class>
                    <method>checkContacts</method>
                </mymodule>
            </observers>
        </controller_action_predispatch_contacts_index_post>
    </events>
</global>
<default>
    <captcha>
        <frontend>
            <areas>
                <contacts>
                    <label>Contacts Page</label>
                </contacts>
            </areas>
        </frontend>
    </captcha>
    <customer>
        <captcha>
            <always_for>
                <contacts>1</contacts>
            </always_for>
        </captcha>
    </customer>
</default>

Create a observer for that

location: app/code/local/MyPackage/MyModule/Model/Observer.php

class MyPackage_MyModule_Model_Observer
{
    public function checkContacts($observer){
        $formId = 'contacts';
        $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
        if ($captchaModel->isRequired()) {
            $controller = $observer->getControllerAction();
            $word = $this->_getCaptchaString($controller->getRequest(), $formId);
            if (!$captchaModel->isCorrect($word)) {
                Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
                $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
                $url =  Mage::getUrl('contacts');
                $controller->getResponse()->setRedirect($url);
            }
        }
        return $this;
    }
    /**
     * Get Captcha String
     *
     * @param Varien_Object $request
     * @param string $formId
     * @return string
     */
    protected function _getCaptchaString($request, $formId)
    {
        $captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
        return $captchaParams[$formId];
    }
}

Create a local.xml to your active theme inside layout folder.

<layout version="0.1.0">
<contacts_index_index>
    <reference name="contactForm">
        <action method="setTemplate"><template>mymodule/contacts/form.phtml</template></action>
        <block type="core/text_list" name="form.additional.info">
            <block type="captcha/captcha" name="captcha">
                <reference name="head">
                    <action method="addJs"><file>mage/captcha.js</file></action>
                </reference>
                <action method="setFormId"><formId>contacts</formId></action>
                <action method="setImgWidth"><width>230</width></action>
                <action method="setImgHeight"><width>50</width></action>
            </block>
        </block>
    </reference>
</contacts_index_index>

Now copy contacts/form.phtml to mymodule/contacts/form.phtml, add <?php echo $this->getChildHtml('form.additional.info'); ?> your requirement. example...

<li class="wide">
            <label for="comment" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Comment') ?></label>
            <div class="input-box">
                <textarea name="comment" id="comment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" class="required-entry input-text" cols="5" rows="3"></textarea>
            </div>
        </li>
        <?php echo $this->getChildHtml('form.additional.info'); ?>
    </ul>

Clear cache. Now Go to System -> Configuration -> Customer Configuration -> Captcha. Select Contact Page and Save.

19
  • 3
    when you copy paste someone's answer you should give credit to the original author. magento.stackexchange.com/a/18620/20064
    – Piyush
    Commented Oct 26, 2017 at 7:05
  • @Piyush I agree with you!!!... Commented Oct 26, 2017 at 7:07
  • @Piyush Hello brother, I didn't copy paste it now. Once I was looking for the answer and yes I got the answer from the link which you've mentioned above. I just noted down this answer in my notes. And today when I saw this question , I wrote the answer from my saved notes. Commented Oct 26, 2017 at 7:16
  • 1
    @DineshYadav, As I mentioned I have created a CMS page (actually I have 5 cms page with different promotions for customer registration) and want to add xml for those cms pages not for contact_index_index, Can you please update your post for my case? Commented Oct 26, 2017 at 7:34
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Commented Oct 26, 2017 at 8:09

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