2014-05-19 26 views
0

我困在ZendFramework 2,annotationbuilder和fileupload的問題。 在聯繫表格我想用戶選擇上傳文件。ZF2文件上傳不需要與註釋生成器

我得到了一切,除了文件上傳的工作,如果沒有文件得到 錯誤:我使用annotationbuilder創建表單 文件沒有上傳

。一些註釋被剪掉了一個測試空間。但沒有幫助。

的註釋類:

<?php 
/** 
* @Annotation\Name("message") 
* @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty") 
* @ORM\Entity 
* @ORM\Table(name="contact_message") 
*/ 
class Message { 



    /** 
    * @Annotation\Exclude() 
    * 
    * @ORM\Id 
    * @ORM\GeneratedValue 
    * @ORM\Column(type="integer") 
    * @var integer 
    */ 
    private $id; 

    /** 
    * @Annotation\Type("Zend\Form\Element\Select") 
    * @Annotation\Flags({"priority": 600}) 
    * @Annotation\Required({"required":"true" }) 
    * @Annotation\Filter({"name":"StringTrim"}) 
    * @ Annotation\Validator({"name":"StringLength"}) 
    * @Annotation\Options({"label":"About:"}) 
    * @Annotation\Attributes({"options":{"1":"PlaceHolder","2":"Test"}}) 
    * 
    * @ORM\Column(type="string") 
    * @var String 
    */ 
    private $about; 

    /** 
    * @Annotation\Type("Zend\Form\Element\Text") 
    * @Annotation\Flags({"priority": 500}) 
    * @Annotation\Required({"required":"true" }) 
    * @Annotation\Filter({"name":"StripTags"}) 
    * @ Annotation\Validator({"name":"EmailAddress"}) 
    * @Annotation\Options({"label":"Name:"}) 
    * @Annotation\Attributes({"required": true,"placeholder": "Your name ... "}) 
    * 
    * @ORM\Column(type="string") 
    * @var String 
    */ 
    private $name; 

    /** 
    * @Annotation\Type("Zend\Form\Element\Text") 
    * @Annotation\Flags({"priority": 500}) 
    * @Annotation\Required({"required":"true" }) 
    * @Annotation\Filter({"name":"StripTags"}) 
    * @ Annotation\Validator({"name":"EmailAddress"}) 
    * @Annotation\Options({"label":"Subject:"}) 
    * @Annotation\Attributes({"required": true,"placeholder": "Subject ... "}) 
    * 
    * @ORM\Column(type="string") 
    * @var String 
    */ 
    private $subject; 

    /** 
    * @Annotation\Type("Zend\Form\Element\File") 
    * @Annotation\Flags({"priority": 500}) 
    * @ Annotation\Required({"required":false }) 
    * @ Annotation\Filter({"name":"StringTrim","filerenameupload":{"target": "./img","randomize":true}}) 
    * @ Annotation\Filter({"name":"StripTags"}) 
    * @ Annotation\Validator({"name":"StringLength"}) 
    * @Annotation\Options({"label":"File:"}) 
    * @Annotation\Attributes({"required": false}) 
    * 
    * @ORM\Column(type="string") 
    * @var String 
    */ 
    private $file; 

    /** 
    * @Annotation\Type("Zend\Form\Element\Textarea") 
    * @Annotation\Flags({"priority": 500}) 
    * @ Annotation\Required({"required":"true" }) 
    * @Annotation\Filter({"name":"StripTags"}) 
    * @ Annotation\Validator({"name":"EmailAddress"}) 
    * @Annotation\Options({"label":"Message:"}) 
    * @Annotation\Attributes({"required": true,"placeholder": "Message ... "}) 
    * 
    * @ORM\Column(type="string") 
    * @var String 
    */ 
    private $message; 

    /** 
    * WARNING USING THESE IS NOT SAFE. there is no checking on the data and you need to know what 
    * you are doing when using these. 
    * But it a great function for lazy people ;) 
    * 
    * @param ANY $value 
    * @param ANY $key 
    * @return $value 
    */ 
    public function __set($value,$key){ 
     return $this->$key = $value; 
    }  

    /** 
    * WARNING USING THESE IS NOT SAFE. there is no checking on the data and you need to know what 
    * you are doing when using these. 
    * But it a great function for lazy people ;) 
    * 
    * @param ANY $value 
    * @param ANY $key 
    * @return $value 
    */ 
    public function __get($key){ 
     return $this->$key; 
    }  

    /** 
    * WARNING USING THESE IS NOT SAFE. there is no checking on the data and you need to know what 
    * you are doing when using these. 
    * This is used to exchange data from form and more when need to store data in the database. 
    * and again ist made lazy, by using foreach without data checks 
    * 
    * @param ANY $value 
    * @param ANY $key 
    * @return $value 
    */ 
    public function populate($array){ 
     foreach ($array as $key => $var){ 
      $this->$key = $var; 
     } 
    } 


    /** 
    * Get an array copy of object 
    * 
    * @return array 
    */ 
    public function getArrayCopy() 
    { 
     return get_object_vars($this); 
    } 


} 
?> 

,並在我的控制器中添加動作:

<?php 

use Zend\View\Model\ViewModel; 
use Zend\Form\Annotation\AnnotationBuilder; 
use Contact\Entity\Contact; 
use Contact\Entity\Company; 
use Contact\Entity\Message; 
use Contact\Controller\EntityUsingController; 

class MessageController extends EntityUsingController { 


    public function addAction(){ 

     $message = new Message(); 

     $builder = new AnnotationBuilder(); 
     $form = $builder->createForm($message); 
     $form->bind($message); 

     $request = $this->getRequest(); 

     if ($request->isPost()) { 

      $form->bind($message); 
      $requestData = array_merge_recursive((array) $request->getPost(),(array) $request->getFiles()); 
      $form->setData($requestData); 
      var_dump($request->getFiles()); 
      if ($form->isValid()) { 
       $em = $this->getEntityManager(); 

       $em->persist($message); 
       $em->flush();     

       $this->flashMessenger()->addMessage('Contact Saved'); 

       return $this->redirect()->toRoute('contact'); 
      } 
     } 

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

    private function storeFile($file){ 

     if (!$this->getConfiguration('fileupload')){ 
      return null; 
     } 

     $fileBank = $this->getServiceLocator()->get('FileRepository'); 
     $entity = $fileBank->save('/tmp/myfile.jpg'); 
    } 
} 
?> 

我希望有人能幫助解決這個問題我搞定。

+0

您確定上傳位置一切正常嗎? – dixromos98

+0

也在你的addAction中,你永遠不會調用storeFile($文件),或者我錯過了什麼? – dixromos98

+0

對不起,存儲還沒有建立,我試圖讓它接受,我沒有選擇任何文件上傳 – KatsuoRyuu

回答

1

這似乎是註解構建器中的一個錯誤。 我在網上發現了很多地方,證實我應該是正確的。 所以我結束了黑客通過添加文件輸入窗體後生成。

if ($this->getConfiguration('fileupload')){ 
    $companyForm->add(array( 
     'name' => 'file', 
     'priority' => 300, 
     'type' => 'file', 
     'options' => array( 
      'label' => 'File:', 
     ), 
    ),array('priority' => 300)); 
} 

檢查配置是否允許文件上傳。一個額外的功能,我把現在我不能設置文件上傳作爲註釋

0

如果你想繼續以另一種方式:

你可以嘗試AllowEmpty註釋:

* @Annotation\AllowEmpty(true) 

或者您可以嘗試禁用控制器中該字段的驗證程序:

$form->getInputFilter()->remove('file'); 
+0

聽起來不錯,我會嘗試與此工作,截至目前我使用刪除過濾器,但我對AllowEmpty更感興趣 – KatsuoRyuu