ProxiBlue / reCaptcha

Clean implementation of Google reCaptcha for magento
http://www.proxiblue.com.au/blog/magento-recaptcha/
GNU General Public License v3.0
86 stars 61 forks source link

Form data clear after successful submit #32

Closed adarshkhatri closed 5 years ago

adarshkhatri commented 5 years ago

Is it normal or it's only happening in my instance?

After successful contact us form submit, data is not cleared from session, which is why it again fills the submitted data in the form.

I had to modify this function:

/**
     * Check Captcha On Contact Us
     *
     * @param Varien_Event_Observer $observer
     *
     * @return Mage_Captcha_Model_Observer
     */
    public function checkContact($observer)
    {
        $formId = 'user_contact';
        $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.'));
                // insert form data to session, allowing to re-populate the contact us form
                $data = $controller->getRequest()->getPost();
                Mage::getSingleton('customer/session')->setFormData($data);
                $controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
                $controller->getResponse()->setRedirect(Mage::getUrl('*/*/'));
            } else{ //success
                //lets clear the form.
                Mage::getSingleton('customer/session')->setFormData(array('name'=> '', 'email' => '', 'telephone' => '', 'comment' => ''));
            }
        }

        return $this;
    }

Is this correct or there is better way?

ProxiBlue commented 5 years ago

Hi,

Never noticed. I do all our contact forms via ajax submissions, and clear the form using javascript. What you did is fine. Can you do a PR?

ProxiBlue commented 5 years ago

You could also try just doing Mage::getSingleton('customer/session')->unsFormData()

adarshkhatri commented 5 years ago

Hi,

Never noticed. I do all our contact forms via ajax submissions, and clear the form using javascript. What you did is fine. Can you do a PR?

Was about to, but Git showed you had already made the changes. 👍 Thanks.