Hi,
I'm facing a CSRF issue: I can only submit the form once, but if I try to submit the form again (e.g. when I forgot to fill field that is required) it won't work and I get 'CSRF-token validation failed'. The same code worked using Phalcon 1.x. Any ideas how to fix this behavior?
MyForm.php
<?php
class MyForm extends Form
{
public function initialize()
{
$csrf = new Hidden('csrf');
// ... some other fields
$csrf->addValidator(new Identical(array(
'value' => $this->security->getSessionToken(),
'message' => 'CSRF-token validation failed'
)));
$this->add($csrf);
}
}
IndexController.php
class IndexController extends Controller
{
public function registerAction()
{
$form = new MyForm();
if ($this->request->isPost())
{
// Validate form against post data
if (!$form->isValid($this->request->getPost()))
{
// Display error messages
foreach ($form->getMessages() as $message)
{
$this->flash->error($message);
}
}
else
{
// ... assign submitted values to model and redirect
}
}
// Assign form to view
$this->view->form = $form;
}
}