问题描述
|
谁能告诉我如何验证另一页上的数据(未在其中创建表单对象)?
事情是:在页面“ A”上,我正在使用自己的验证器创建表单对象,并向用户显示该表单。但是操作转到页面“ B”,我需要在其中验证数据。
我想做这样的事情(页面“ B”):
$form = new someForm();
$form->bind($this->getRequest()->getParameter(\'data\'));
if($form->isValid())
{
print \'true\';
}
else
{
print \'false\';
}
但您可以想象,它将显示\'false \'。
解决方法
我想这是由于Symfony中CSRF对表单的保护
尝试使用此代码
$form = new someForm();
$form->disableLocalCSRFProtection();
$form->bind($this->getRequest()->getParameter(\'data\'));
if($form->isValid())
{
print \'true\';
}
else
{
print \'false\';
}
, 也许您可以像这样解决此问题:
public function executeFoo($request){
$this->form = new fooForm();
$this->getUser()->setAttribute(\'tmpForm\',$this->form);
}
在您的表单中,操作必须指向模块/栏
在那里你可以做:
public function executeBar($request){
$this->forward404Unless($form = $this->getUser()->getAttribute(\'tmpForm\'));
$form->bind($this->getRequest()->getParameter(\'data\'))
// and so on
}