Pergunta

In my product review form the form key fetched from core/session is cached and does not pass the validation upon form submission. So,

Mage::getSingleton('cor/session')->getFormKey() and $this->getRequest()->getParam('form_key') return different values.

I don't want to switch off block's caching because it will impact on performance. Is there any way to get actual form key in js? Is it stored somewhere as js variable?

Or is there any other way i can get valid form key without switching the cache off?

Foi útil?

Solução

You could perform an ajax request on page load asking for a key from an uncached controller. Use jquery with a controller that responds with json. That should work.

Create extension with controller like this

class ArchApps_JsonResponse_IndexController  
    extends Mage_Core_Controller_Front_Action
{
    /**
     * Example of single controller action returning JSON response
     */
    public function jsonAction()
    {
        // Data to be converted to JSON and returned as response
        $config = array('data' => array(1, 2, 3, 4, 5));

        // Make sure the content type for this response is JSON
        $this->getResponse()->clearHeaders()->setHeader(
            'Content-type',
            'application/json'
        );

        // Set the response body / contents to be the JSON data
        $this->getResponse()->setBody(
            Mage::helper('core')->jsonEncode($config)
        );
    }
}

No block content so avoiding cache

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top