我试图用crsf令牌保护我的Zend表单。总之如果我将令牌元素添加到我的表单中,它总是向我发回notEmpty令牌的错误消息。我做错了什么? THX
class Application_Form_Test3 extends Zend_Form {
public function init() {
$this->setMethod('post');
//..some elements
$note = new Zend_Form_Element_Textarea('note');
$note->addValidator('stringLength', false, array(2, 50));
$note->setRequired(true);
$note->class = 'form-control';
$note->setLabel('Poznámka:');
$note->setAttrib('placeholder', 'poznamka ke spisu');
$note->setOptions(array('cols' => '20', 'rows' => '4'));
$submit = new Zend_Form_Element_Submit('submit');
$submit->class = 'btn btn-success';
$submit->setValue('odeslat');
$this->addElements(array(
$number,
$year,
$owner,
$note,
$submit,
));
$this->addElement('hash', 'no_csrf_foo', array('salt' => 'unique'));
}
}
控制器中的动作:
public function findAction() {
$request = $this->getRequest();
$form = new Application_Form_Test3();
if ($this->getRequest()->isPost()) {
if ($form->isValid($request->getPost())) {
var_dump($request->getPost());
} else {
var_dump("ERROR");
}
}
$this->view->form = $form;
}
在我看来,我渲染表单并转储错误消息
...
<?php echo $form->renderForm(false); ?>
...
//render single elements here
//eg. <?php echo $form->note->renderViewHelper(); ?>
...
<?php var_dump($form->getMessages()) ?>
...
每次表单验证后,我都会得到一些错误消息:
array(2) { ["note"]=> array(1) { ["isEmpty"]=> string(36) "Value is required and can't be empty" } ["no_csrf_foo"]=> array(1) { ["isEmpty"]=> string(36) "Value is required and can't be empty" } }
如果我给元素填充了好的值,那么最后一个错误始终是令牌 - NotEmpty,所以我的表单永远不会有效。
答案 0 :(得分:0)
问题解决了。我没有在我的视图中渲染令牌元素,所以我添加到View:
<?php echo $form->no_csrf_foo->renderViewHelper(); ?>