简体   繁体   English

在zend Framework 2中使用文件输入编辑表单

[英]Edit form with file input in zend framework 2

I use fielrenameupload validation and fileprg to upload file in my form . 我使用fielrenameupload验证和fileprg来上传表单中的文件。 It works very well but in edit action validation fails for file input How I can solve this problem in edit action ? 它工作得很好,但是在编辑操作中,文件输入的验证失败。如何解决编辑操作中的此问题?

this is edit action that works great for add action : 这是编辑操作,非常适合添加操作:

public function editAction()
    {
        $id = $this->params()->fromRoute('id',0);
        $category = $this->categoryTable->get($id);

        $form = $this->getServiceLocator()->get('CategoryForm');

        $prg = $this->fileprg($form);
        $form->bind($category);

        if ($prg instanceof \Zend\Http\PhpEnviroment\Response)
        {
            return $prg;
        }
        elseif (is_array($prg))
        {
            if ($form->isValid())
            {
                $data = $form->getData();
                $data['image'] = $data['image']['tmp_name'];
                $category = new CategoryEntity;
                $category->exchangeArray($data);

                if ($this->categoryTable->save($category))
                    $this->redirect()->toRoute(null,array('action'=>'index'));
            }
        }

        $view = new ViewModel(array(
            'form'  =>  $form,
        ));
        $view->setTemplate('category/admin/add');
        return $view;
    }

And this is validation code for file input : 这是文件输入的验证代码:

$image = new \Zend\InputFilter\FileInput('image');
        $image->getFilterChain()->attachByName('filerenameupload',array(
            'target'    =>  'data/upload/images/category',
            'use_upload_name'   =>  true,
            'randomize'     =>  true,
        ));

The add action passes the $form->isValid() validation because there is a image file posted in the form submit. add操作通过$form->isValid()验证,因为在表单提交中发布了一个图像文件。 However, in the edit action, it's empty. 但是,在编辑操作中,它为空。

In order to avoid avoid the problem you're facing try the following: 为了避免遇到您遇到的问题,请尝试以下操作:

$imageFilter = $form->getInputFilter()->get( 'image' );
$imageFilter->setRequired( false );

Just be sure to place this lines before the $form->isValid() line. 只要确保将这些行放在$form->isValid()行之前即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM