0

I found an app that adds a captcha to the contact page - it works well.

But it doesn't add it to the other custom forms I've built.

<layout version="0.1.0">


<contacts_index_index translate="label">
    <reference name="content">

        <reference name="contactForm">
            <action method="setTemplate"> 
                <template>contacts/contactscaptcha.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>contact_us</formId></action>
                    <action method="setImgWidth"><width>230</width></action>
                    <action method="setImgHeight"><width>50</width></action>
                </block>
            </block>
        </reference>



    </reference>
</contacts_index_index>

My thinking was to add something like:

    <formABPOA translate="label">
    <reference name="content">

        <reference name="formABPOA">
            <action method="setTemplate"> 
                <template>contacts/formABPOA.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>formABPOA</formId></action>
                    <action method="setImgWidth"><width>230</width></action>
                    <action method="setImgHeight"><width>50</width></action>
                </block>
            </block>
        </reference>


    </reference>
</formABPOA>

3 Answers 3

1

Please check with below url - Add you custom page in admin like below url.

https://www.eglobeits.com/blog/magento-captcha-in-contact-us-form-and-custom-forms/

its very simple way to add captcha in custom page.

I hope its work for you.

1

You need to create module in order to add captcha in custom form. After creating module you need go to System -> Configuration -> Customer Configuration -> Captcha -> Select Contact Page and Save

First add this file app/etc/modules/MyPackage_MyModule.xml

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

After that Create configuration file for your module

Path: 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>

You also need to create a observer for that

Path should be: 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 in your layout folder of active theme.

<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'); ?>

<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>

At last Clear your cache.

I hope this will help

1
  • I have this already and it works well on the contact page. But I cant get my head around getting in on a 2nd page? contacts/formABPOA.phtml form.additional.info is added, observer is added, config its added as well. Jul 11, 2019 at 23:49
1

I think this link Magento: Add captcha to custom contact form should be work for you. Using @sohel rana's answer you can able to add captcha in anywhere in your custom form. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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