不起作用:使用 ZF2 进行文件上传验证



我正在开发一个图像库,我想检查输入文件是否设置了文件。

这是我的尝试,只检查标题,但如果用户没有设置图像,则未检测到它,我做错了什么?

形式
namespace BackofficeForm;
use ZendFormForm;
class GalerieForm extends Form
{
public function __construct($galerieContent = null)
{
parent::__construct('galerie-form');
$this->setAttribute('action', '/backoffice/galerie/add');
$this->setAttribute('method', 'post');
$this->setAttribute('role', 'form');
$this->setAttribute('enctype', 'multipart/form-data');
$this->setInputFilter(new BackofficeFormGalerieFilter());
$this->add(array(
'name' => 'title',
'attributes' => array(
'type' => 'text',
'id' => 'title',
'value' => $galerieContent->title,
'class' => 'form-control'
),
'options' => array(
'label' => 'Picture Title:',
'label_attributes' => array(
'class' => 'control-label'
)
),
));
$this->add(array(
'name' => 'picture',
'attributes' => array(
'type' => 'file',
'id' => 'picture-selector',
'value' => $galerieContent->picture,
'class' => 'btn btn-file',
),
'options' => array(
'label' => 'Picture:',
'label_attributes' => array(
'class' => 'col-xs-1 control-label',
)
),
));
$this->add(array(
'name' => 'update-from',
'attributes' => array(
'type' => 'hidden',
'value' => 'galerie'
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Update Gallery Content',
'class' => 'btn btn-primary'
),
));
}
}
输入过滤器
namespace BackofficeForm;
use ZendForm;
use ZendInputFilterInputFilter;
use ZendValidatorFileIsImage;
class GalerieFilter extends InputFilter
{
public function __construct()
{
$this->add(array(
    'name' => 'title',
    'required'=> true,
    'filters' => array(
       array('name' => 'StripTags'),
       array('name' => 'StringTrim'),
     ),
     'validators' => array(
         array(
           'name' => 'StringLength',
           'options' => array(
           'encoding' => 'UTF-8',
           'min' => 1,
           'max' => 255,
         ),
      ),
),
));
$this->add(array(
  'name' => 'picture',
  'required'=> true
));
}
}

控制器

public function addAction()
{
if ($this->getRequest()->isPost())
{
    $post = array_merge_recursive(
      $this->getRequest()->getPost()->toArray(),
      $this->getRequest()->getFiles()->toArray()
    );
    var_dump($post);
    $form = new BackofficeFormGalerieForm();
    $form->setData($post);
    if ($form->isValid()) {
      var_dump($post);
    }
}
else {
   $form = new BackofficeFormGalerieForm();
}
return new ViewModel(array(
   'form' => $form
));
}

我之前遇到过同样的问题,然后我把它放进了我的控制器:

if ($request->isPost()) {
        $post = array_merge_recursive(
            $request->getPost()->toArray(),
            $request->getFiles()->toArray()
        );
        // To get the required error message
        if (!$post['picture']['tmp_name']) {
            $post['picture'] = null;
        }
        $form->setData($post);
 }

我建议使用额外的Valitator扩展您的InputFilter,例如UploadFile,它检查是否有上传的文件。这比在控制器操作中定义其他验证规则更易于维护。

输入过滤器代码..

$this->add(array(
  'name' => 'picture',
  'required' => true,
  'validators' => array(
      new ZendValidatorFileUploadFile()
   )
)

ZF2 有几个用于文件验证的标准验证器,并且已经有 InputFilters,特别是用于文件上传。

最新更新