簡體   English   中英

無法正常運作:使用ZF2進行FileUpload驗證

[英]Not working: FileUpload validation with ZF2

我正在開發圖片庫,我想檢查輸入文件是否有文件集。

這是我的嘗試,僅檢查標題,但如果用戶未設置圖像,則不會檢測到該圖像,這是我做錯了什么?

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

之前我遇到過同樣的問題,然后將其放入控制器中:

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,它檢查是否有上傳的文件。 這將比在控制器操作中定義其他驗證規則更具維護性。

InputFilter代碼

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

ZF2有幾個用於文件驗證的標准驗證器,並且已經有InputFilters(特別是用於文件上傳)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM