Native CAPTCHA settings
First of all find Customer Configuration and open CAPTCHA settings
How to add new form to CAPTCHA settings?
Create new module and add this lines to config.xml:
<?xml version="1.0"?> <config> ... <global> <events> <controller_action_predispatch_routename_controller_method> <observers> <module> <class>module/observer</class> <method>checkCaptcha</method> </module> </observers> </controller_action_predispatch_routename_controller_method> </events> </global> <default> <captcha> <frontend> <areas> <contact_form> <label>Contact Form</label> </contact_form> </areas> </frontend> </captcha> <customer> <captcha> <always_for> <contact_form>1</contact_form> </always_for> </captcha> </customer> </default> </config>
You might found here module/observer. It is explained further.
How to show CAPTCHA in contacts form?
Find your form template and insert:
<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post" class="form">
...
<?php echo $this->getChildHtml('captcha'); ?>
...
</form>
After that find layout xml for your page, for example mine was contacts.xml:
<layout version="0.1.0"> <contacts_index_index translate="label"> <label>Contact Us Form</label> <reference name="root"> <action method="setTemplate"><template>page/3columns.phtml</template></action> <action method="setHeaderTitle" translate="title" module="contacts"><title>Contact Us</title></action> </reference> <reference name="content"> <block type="core/template" name="contactForm" template="contacts/form.phtml"> <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> </layout>
How to check CAPTCHA?
Create Observer for your module:
<?php
class Your_Module_Model_Observer
{
/**
* 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];
}
/**
* Break the execution in case of incorrect CAPTCHA
*
* @param Varien_Event_Observer $observer
* @return Your_Module_Model_Observer
*/
public function checkCaptcha($observer)
{
$formId = 'contact_form';
$captchaModel = Mage::helper('captcha')->getCaptcha($formId);
if ($captchaModel->isRequired()) {
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
$controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
Mage::getSingleton('customer/session')->setCustomerFormData($controller->getRequest()->getPost());
$controller->getResponse()->setRedirect(Mage::getUrl('*/*/index'));
}
}
return $this;
}
}