File io 在zend framework 2中使用文件输入编辑表单
我使用fielrenameupload验证和fileprg以我的表单上传文件。 它工作得很好,但在编辑操作中,文件输入验证失败 如何在编辑操作中解决此问题 这是一个编辑操作,非常适合添加操作:File io 在zend framework 2中使用文件输入编辑表单,file-io,zend-framework2,edit,File Io,Zend Framework2,Edit,我使用fielrenameupload验证和fileprg以我的表单上传文件。 它工作得很好,但在编辑操作中,文件输入验证失败 如何在编辑操作中解决此问题 这是一个编辑操作,非常适合添加操作: public function editAction() { $id = $this->params()->fromRoute('id',0); $category = $this->categoryTable->get($id);
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;
}
这是文件输入的验证代码:
$image = new \Zend\InputFilter\FileInput('image');
$image->getFilterChain()->attachByName('filerenameupload',array(
'target' => 'data/upload/images/category',
'use_upload_name' => true,
'randomize' => true,
));
添加操作通过了
$form->isValid()
验证,因为表单submit中发布了一个图像。但是,在编辑操作中,它是空的
为了避免您面临的问题,请尝试以下操作:
$imageFilter = $form->getInputFilter()->get( 'image' );
$imageFilter->setRequired( false );
只需确保将此行放在$form->isValid()行之前即可