我一直在关注文件上传的ZendFramework文档: http://framework.zend.com/manual/2.1/en/modules/zend.form.file-upload.html
我的问题是,当我尝试在无效时提交表单时,我收到以下错误消息:
Array provided to Escape helper, but flags do not allow recursion
以下是我的控制器中特定操作的代码:
public function addAction()
{
$form = new TeamForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if ($request->isPost()) {
$team = new Team();
$form->setInputFilter($team->getInputFilter());
$post = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
$form->setData($post);
if ($form->isValid()) {
$files = $request->getFiles();
$filter = new RenameUpload(array("target" => "./public/uploads/", "use_upload_extension" => true, "randomize" => true));
$fileinfo = $filter->filter($files['image']);
$team->exchangeArray($form->getData());
$team->image = basename($fileinfo["tmp_name"]);
$this->getTeamTable()->saveTeam($team);
return $this->redirect()->toRoute('team');
}
}
return array('form' => $form);
}
我将错误缩小到以下行:
$form->setData($post);
当我执行$ post的变量转储时,一切看起来都是正确的。在互联网上搜索之后,我无法找到任何答案,说明为什么会这样。
如果有必要,我很乐意提供更多信息。
谢谢,
修改
这是视图代码
<?php
$form->setAttribute('action', $this->url('team', array('action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formInput($form->get('image'));
echo $this->formInput($form->get('name')
->setAttribute('class', 'large m-wrap')
->setAttribute('autocomplete', 'off')
->setAttribute('placeholder', 'Name'));
echo $this->formElementErrors($form->get('name'));
echo $this->formInput($form->get('title')
->setAttribute('class', 'large m-wrap')
->setAttribute('autocomplete', 'off')
->setAttribute('placeholder', 'Title'));
echo $this->formElementErrors($form->get('title'));
echo $this->formSubmit($form->get('submit')
->setAttribute('class', 'btn green'));
echo $this->form()->closeTag();
?>
答案 0 :(得分:2)
问题出在您的视图文件中,请使用
<?php echo $this->formFile($form->get('image')); ?>
而不是
echo $this->formInput($form->get('image'));
对于文件类型,它应该是$this->formFile()
答案 1 :(得分:0)
问题不在控制器中,而在视图中。您正在将数组传递给视图助手escape()
而不是字符串。
答案 2 :(得分:-1)
我认为问题在于上面的行,执行array_merge而不是递归合并。