2016-05-09 37 views
2

我一直在適應這個例子我自己的形式:http://framework.zend.com/manual/2.1/en/modules/zend.form.file-upload.html爲什麼ZF2 PRG插件返回[fileUploadFileErrorFileNotFound] =>文件未找到?

當我加入我的形式使用Choose File按鈕我的文件,然後提交表單,我得到這個:

Array 
(
    [fileUploadFileErrorFileNotFound] => File was not found 
) 

和變量其內容打印在上面是這個(表格提交後):

$fileErrors = $form->get('character-ref')->getMessages(); 

可能是什麼問題?我有點不知道這一點。我看着RenameUpload過濾器和一些網站,但在這一點上缺乏線索。

我的代碼,其中有關:

class CommissionInputFilter implements InputFilterAwareInterface 
{ 
    function getInputFilter() 
    { 
     $inputFilter = new InputFilter(); 

     //this is the filter that is supposed to make 
     //PRG Plugin file upload possible 
     $renameFilter = new RenameUpload(array(
      'target' => "./data/uploads/", 
      'randomize' => true 
     )); 

     // File Input - I am not sure if this is the way to do it 
     //some examples do not use FileInput 
     $fileInput = new FileInput('character-ref'); 
     $fileInput->setRequired(false); 
     $fileInput->getFilterChain()->attach($renameFilter); 
     $inputFilter->add($fileInput); 
     return $inputFilter; 
    } 
} 


//code inside controller 

public function addAction() 
{ 
    $form = new CommissionForm(); 
    $tempFile = null; 

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

    if ($prg instanceof \Zend\Http\PhpEnvironment\Response) { 
     return $prg; 
    } elseif (is_array($prg)) { 

     //************************************************** 
     //this line below is where I set my input filter for the form 
     //the filter that contains PRG Rename pluging supposedly 
     //One interesting note is if I *Remove* the line below 
     //then the PRG Plugin keeps the file name as it is supposed to 
     //************************************************** 

     $form->setInputFilter((new CommissionInputFilter())->getInputFilter()); 
     if ($form->isValid()) { 

      $output = new CommissionOutput(); 

      $commission = $output->getCommission($form->getData()); 
      $id = $this->repository->saveCommission($commission); 

      $view = new ViewModel(); 
      $view->setTemplate('commission/commission/thank_you'); 
      $view->setVariables($prg); 
      return $view; 
     } else { 

      //******************************************************** 
      //My Properly-filled out form ends up here with file errors 
      //******************************************************** 

      $fileErrors = $form->get('character-ref')->getMessages(); 
      if (empty($fileErrors)) { 
       $tempFile = $form->get('character-ref')->getValue(); 
      } 
     } 
    } 
    return array(
     'form' => $form, 
     'characterRefFile' => $tempFile 
    ); 
} 

回答

0

哦,嗯,我想發佈相關信息的幫助。

因爲我找到了原因。顯然,

  1. 形式需要正確設置窗體篩選[做過]
  2. PRG需要RenameUpload過濾器設置[做過]
  3. 最重要的是,表單過濾器必須建立表單後成立和PRG前[沒有做到這一點]
$inputFilter = (new CommissionInputFilter())->getInputFilter(); 

$form = new CommissionForm(); 
$form->setInputFilter($inputFilter); 
$prg = $this->fileprg($form); 

過濾一定形式創建後和前PRG插件耶成立。