Magento Enable Captcha For Contact Us Form

Magento has default contact us form and by default is had been disabled, you need to enable it from the admin configuration. The same way Magento also has CAPTCHA and by default it is disabled.

Find below steps to enable the Contact us form.

Step-1 : On the Admin menu, select System > Configuration.

Step-2 : In the panel on the left, under General, click Contacts.

Step-3 : Click to expand the Contact Us section, and set Enable Contact Us to Yes.

Magento enable contact us

To Enable CAPTCHA, Follow the below steps

Step-1 : On the Admin menu, select System -> Configuration -> ADVANCED ->Admin.

Step-2 : Click to expand the CAPTCHA section, and Enable CAPTCHA in Admin to Yes. There you can find “Forms” and “Displaying Mode”, Find Below image for your reference

Magento enable captcha

Step-3 : Under Forms, you could not find “Contact Us” form, To add Contact Us form, find captcha/forms element in core_config table and add “contact_form” form name to the list and modify config.xml in

/app/code/core/Mage/Captcha/etc

in – <frontend><areas> add something like

<contact_form>
<label>Contact Us</label>
</contact_form>

Step-4 : and in – <always_for> add the below

<contact_form>1</contact_form>

Also there’s a miss for translation in the config file, so you should add this to the <frontend> section:

<translate>
<modules>
<Mage_Captcha>
<files>
<default>Mage_Captcha.csv</default>
</files>
</Mage_Captcha>
</modules>
</translate>

Step-5 : Refresh cache. Login to Admin, and make sure Contact Us form is listed in CAPTCHA configuration

Step-6 : Modify captcha layout xml file in frontend/base/default (or your theme) and add this section

<contacts_index_index>
<reference name=”contactForm”>
<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_form</formId>
</action>
<action method=”setImgWidth”>
<width>230</width>
</action>
<action method=”setImgHeight”>
<width>50</width>
</action>
</block>
</block>
</reference>
</contacts_index_index>

and Refresh cache.

Step-7 : Modify form code in /frontend/base/default/template/contacts/form.phtml to add placeholder to render captcha layout .Add this line of code right before </ul>

<?php echo $this->getChildHtml(\\\’form.additional.info\\\’); ?>

Step-8 : Now, how to “trap” the post from the Contact Us form and check the captcha
Mage uses Events and Observer to achieve this, and it works very well for the forms/events they have implemented this feature. I was not able to find events specific for Contact Us form, so, IMHO the most obvious solution is to override indexController in code/core/Mage/Contacts/controllers and in the postAction method, inside try block add the following code:

$formId = ‘contact_form';
$captchaModel = Mage::helper(‘captcha’)->getCaptcha($formId);
if ($captchaModel->isRequired()) {
if (!$captchaModel->isCorrect($this->_getCaptchaString($this->getRequest(), $formId))) {
Mage::getSingleton(‘customer/session’)->addError(Mage::helper(‘captcha’)->__(‘Incorrect CAPTCHA.’));
$this->setFlag(”, Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
Mage::getSingleton(‘customer/session’)->setCustomerFormData($this->getRequest()->getPost());
$this->getResponse()->setRedirect(Mage::getUrl(‘*/*/’));
return;
}
}

and add this utility method to the controller class:

protected function _getCaptchaString($request, $formId)
{
$captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE);
return $captchaParams[$formId];
}

Step-9 : Refresh Cache, You are done.

4 Comments

  1. Deepak K.

    Hi,

    Also, when I checked the form id using firebug its showing “contactForm” instead “contact_form”. I’m using magento 1.9. Please help.

  2. Susanta Kumar Das

    Thanks,

  3. Susanta Kumar Das

    It really Help full

  4. Thank you very much. This is exactly what i am looking for. Worked perfectly! Thaks!

Leave a Comment