use:
use Phalcon\Forms\Element\Hidden;
use Phalcon\Validation\Validator\Identical;
class UserRegisterForm extends Form
{
public function initialize($entity = null, $options = null)
{
//... code ...
// CSRF
$csrf = new Hidden('csrf');
$csrf->addValidator(new Identical([
'accepted' => $this->security->getSessionToken(),
'message' => 'CSRF validation failed'
]));
$this->add($csrf);
}
}
Controler o Micro:
$params = [];
$form = new UserRegisterForm();
if ($this->request->isPost()) {
if ($form->isValid($this->request->getpost()) != false) {
// ... code ...
if ($user->save()) {
return $this->response->redirect(['for' => 'user.register.success']);
}
}
}
// important
$this->view->form = $form;
$this->tag->setTitle("Register");
echo $this->view->render('user/register', $params);
Template:
{{ form.render('csrf', ['value': security.getToken()]) }}