使用验证码

时间:2016-02-01 10:50:25

标签: magento session

我在magento中创建了一个自定义表单扩展。提交表单后,如果验证码不正确,如何预填表单值。表单不会保存在后端。这是我的控制器文件,

class namespace_moduleName_IndexController extends

Mage_Core_Controller_Front_Action {

const XML_PATH_EMAIL_SENDER ='contacts / email / sender_email_identity';

 public function indexAction() {


    $this->loadLayout();
    $this->renderLayout();
}

public function postAction() {

      $formId = 'form_id';         
    $captchaModel = Mage::helper('captcha')->getCaptcha($formId);

    if ($captchaModel->isRequired()) {
        if (!$captchaModel->isCorrect($this->_getCaptchaString($this->getRequest(), $formId))) {
            Mage::getSingleton('core/session')->addError(Mage::helper('form_id')->__('The CAPTCHA you entered is not correct. Go back and try it again'));
            $this->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
           // Mage::getSingleton('customer/session')->setCustomerFormData($this->getRequest()->getPost());
            $this->_redirectReferer();
            return;
        }
    } 

     $post = $this->getRequest()->getPost();
       if ($post) {
            $translate = Mage::getSingleton('core/translate');
            /* @var $translate Mage_Core_Model_Translate */
            $translate->setTranslateInline(false);
                try {
                    $postObject = new Varien_Object();
                    $postObject->setData($post);

                    $error = false;
                if (!Zend_Validate::is(trim($post['name']), 'NotEmpty')) {
                        $error = true;
                    }
                if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                        $error = true;
                   }

                if (!Zend_Validate::is(trim($post['message']), 'NotEmpty')) {
                       $error = true;
                  }

                if ($error) {
                        throw new Exception();
                    }
                        $mailTemplate = Mage::getModel('core/email_template');
                        /* @var $mailTemplate Mage_Core_Model_Email_Template */
                        $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                                    ->setReplyTo($post['email'])
                                    ->sendTransactional(
                                        Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                                        Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                                        Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                                        null,
                                        array('data' => $postObject)
                        );

                        if (!$mailTemplate->getSentSuccess()) {
                            throw new Exception();
                        }
                        Mage::getSingleton('core/session')->addSuccess(Mage::helper('form_id')->__('Your enquiry for the product was submitted and will be responded to as soon as possible. Thank you for contacting us. '));
                        $this->_redirectReferer();    

                } catch (Exception $e) {

                    Mage::getSingleton('core/session')->addError(Mage::helper('form_id')->__('Unable to submit your request. Please, try again later'));
                    $this->_redirectReferer();
                }

            } 
            else {
                Mage::getSingleton('core/session')->addError(Mage::helper('form_id')->__('Unable to submit your request. Please, try again later'));
               $this->_redirectReferer();
            }
        }

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

}

1 个答案:

答案 0 :(得分:0)

在验证和重定向之前。您可以将提交的数据保存在Registry中。并且您可以在表单详细信息页面上访问已保存的注册表数据。

发送电子邮件时,您可以删除注册表数据。

$post = $this->getRequest()->getPost();

注册变量

Mage::register('name', $post['name']);
Mage::register('email', $post['email']);
Mage::register('message', $post['message']);

使用以下方法在模板中获取注册表变量值

Mage::registry('name');
Mage::registry('email');
Mage::registry('message');

销毁/取消注册变量

Mage::unregister('name');
Mage::unregister('email');
Mage::unregister('message');