php - ZendFramework 2 表单文件验证器

标签 php zend-framework2

我一直在关注文件上传的 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

这是我的 Controller 中特定操作的代码:

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();
?>

最佳答案

问题出在你的 View 文件中,请使用

<?php echo $this->formFile($form->get('image')); ?>

代替

echo $this->formInput($form->get('image'));

文件类型应该是$this->formFile()

关于php - ZendFramework 2 表单文件验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19283563/

相关文章:

php - Magento 2 : How to get website Url by website Id in Observer

javascript - 如何使网格中的背景图像可点击?

php - Zend framework 2/Doctrine 2/批量操作和事件触发

localization - 在 Zend Framework 2 中获取布局或 View 的本地值

php - 在 ZF2 中,如何注册自定义 Feed Reader 扩展?

javascript - 重复页面显示不同

php - Cakephp2 FindBy语句中的sql错误,得到1064语法错误

php - TCPDF - 如何调整标题的高度?

json - ZF2 查看策略

zend-framework2 - ZF2-Helper 在应用程序目录中不起作用