不工作:使用ZF2进行FileUpload验证

时间:2014-01-10 11:58:53

标签: php zend-framework2

我正在开发图片库,我想检查输入文件是否有文件集。

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

形成
namespace Backoffice\Form;

use Zend\Form\Form;

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 \Backoffice\Form\GalerieFilter());

$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 Backoffice\Form;

use Zend\Form;
use Zend\InputFilter\InputFilter;
use Zend\Validator\File\IsImage;

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 \Backoffice\Form\GalerieForm();
    $form->setData($post);

    if ($form->isValid()) {
      var_dump($post);
    }
}
else {
   $form = new \Backoffice\Form\GalerieForm();
}

return new ViewModel(array(
   'form' => $form
));

}

2 个答案:

答案 0 :(得分:2)

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

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);
 }

答案 1 :(得分:2)

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

InputFilter code ..

$this->add(array(
  'name' => 'picture',
  'required' => true,
  'validators' => array(
      new \Zend\Validator\File\UploadFile()
   )
)

ZF2为File validation提供了多个标准验证器,并且InputFilters已经{{3}},尤其是文件上传。