Create, besides the form that will contain the data, a form without
elements. From the controller you instantiate the two forms. Also in the
controller, you add the element hash to the empty form. Both forms should be
sent to the vision. Then, in the condition "if ($ request-> isXmlHttpRequest
())" in the controller you render the empty form. Then, you take the hash
value with the method "getValue ()". This value must be sent in response by
Ajax and then use JavaScript to replace the hash value that is already
obsolete. The option to create an empty form for the hash is to avoid
problems with other elements such as, for example, the captcha that would
have its id generated again if the form were rendered, and would also need
to have the new information replaced. The validation will be done separately
because there are two distinct forms. Later you can reuse the hash (empty)
form whenever you want. The following are examples of the code.
//In the controller, after instantiating the empty form you add the Hash
element to it:
$hash = new Zend_Form_Element_Hash('no_csrf_foo');
$hash_form->addElement('hash', 'no_csrf_foo', array('salt' => 'unique'));
//...
//Also in the controller, within the condition "if
($request->isXmlHttpRequest())" you render the form (this will renew the
session for the next attempt to send the form) and get the new id value:
$hash_form->render($this->view);
$hash_value['hash'] =
$hash_form->getElement('no_csrf_foo')->getValue();//The value must be added
to the ajax response in JSON, for example. One can use the methods
Zend_Json::decode($response) and Zend_Json::encode($array) for conversions
between PHP array and JSON.
//---------------------------------------
//In JavaScript, the Ajax response function:
document.getElementById("no_csrf_foo").value = data.hash;//Retrieves the
hash value from the Json response and set it to the hash input.
--
View this message in context: http://n4.nabble.com/Problem-with-Zend-Form-Element-Hash-tp668669p1747689.html
Sent from the Zend MVC mailing list archive at Nabble.com.
没有评论:
发表评论