2

i have one extesnion for vendor. when customer create account it shows this vendor register form in which i want to use captcha.

i used this reference link for capthca. captcha shows bt not working.

Config.xml

<events>
        <controller_action_predispatch_multivendor_vendor_captcha>
             <observers>
                <multivendor>
                    <class>multivendor/Captchaobserver</class>
                    <method>checkCaptcha</method>
                </multivendor>
            </observers> 
        </controller_action_predispatch_multivendor_vendor_captcha>
    </events>

multivendor.xml

<multivendor_vendor_signup>
     <label>Customer Account Registration Form</label>
    <!-- Mage_Customer -->
    <remove name="right"/>
    <remove name="left"/>

    <reference name="root">
        <action method="setTemplate"><template>page/1column.phtml</template></action>
    </reference>
    <reference name="content">
        <block type="customer/form_register" name="multivendor_customer_form_register" template="multivendor/form/register.phtml">
            <action method="setShowAddressFields"><value>true</value></action>
            <block type="page/html_wrapper" name="multivendor.customer.form.register.fields.before" as="multivendor_form_fields_before" translate="label">
                <label>Form Fields Before</label>

            </block>

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

Observer.php

class CompanyName_MultiVendor_Model_Captchaobserver
{

    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('*/*/signup'));
        }
    }

    return $this;
}

}

2

layout.xml

<reference name="content">
    <block type="customer/form_register" name="multivendor_customer_form_register" template="multivendor/form/register.phtml">
        <action method="setShowAddressFields"><value>true</value></action>
        <block type="page/html_wrapper" name="multivendor.customer.form.register.fields.before" as="multivendor_form_fields_before" translate="label">
            <label>Form Fields Before</label>
        </block>


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


    </block>
</reference>

form.phtml

<div class="field">
    <?php echo $this->getChildHtml('captcha'); ?>
</div>

controller.php in submit action (where your form submitted)

//Captcha Checking Start
$formId = 'vendor_form';
$captchaModel = Mage::helper('captcha')->getCaptcha($formId);
if ($captchaModel->isRequired()) {
    $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
    if ($captchaModel->isRequired()) {
        if (!$captchaModel->isCorrect($this->_getCaptchaString($this->getRequest(), $formId))) {
            Mage::getSingleton('core/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
            $this->_redirectReferer('');
            return;
        }
    }
}
//Captcha Checking End

Enjoy it works great.

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.