2014-02-28 62 views
0

因此,我創建了一個表單,並且希望在我的項目中使用它。我使用了Zend Framework 2Doctrine Orm。提交表單時出現問題:我什麼都沒有,這意味着表單沒有提交。有關更多詳細信息,我將編寫我的代碼。所以如果有人有任何解決方案,我會非常感激。無法在Zend Framework中驗證我的表單2

這是我的實體:

class Article implements InputFilterAwareInterface 
{ 
    protected $inputFilter; 

    /** 
    * @ORM\Column(name="publication", type="boolean") 
    */ 
    protected $publication; 

    public function __construct() 
    { 
     $this->date = new \Datetime(); 
} 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="title", type="string", length=255) 
    */ 
    protected $title; 

    // .... 

    /** 
    * Populate from an array. 
    * 
    * @param array $data 
    */ 
    // here maybe the data can't pass from my form to the entity !! 
    public function populate($data = array()) 
    { 
     $this->content = $data['content']; 
     $this->title = $data['title']; 
     $this->date = $data['date']; 
     $this->publication = $data['publication']; 
     $this->image = $data['image']; 
    } 

    public function setInputFilter(InputFilterInterface $inputFilter) 
    { 
     throw new \Exception("Not used"); 
    } 

    public function getInputFilter() 
    { 
     if (!$this->inputFilter) { 
      $inputFilter = new InputFilter(); 

      $factory = new InputFactory(); 

      $inputFilter->add($factory->createInput(array(
       'name'  => 'content', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
      ), 
       'validators' => array(
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min'  => 60, 
        ), 
       ), 
      ), 
     ))); 

      // .... 
    } 
} 

然後我的行動:

$form = new ArticleForm(); 
$form->get('submit')->setAttribute('label', 'Add'); 
$request = $this->getRequest(); 

if ($this->zfcUserAuthentication()->hasIdentity()) { 
    if ($request->isPost()) 
    { 
     $article = new Article(); 
     $form->setInputFilter($article->getInputFilter()); 
     $form->setData($request->getPost()); 
     if ($form->isValid()) { 
      $article->populate($form->getData());// here i think i have a problem 
      $this->getObjectManager()->flush(); 
      $newId = $article->getId(); 

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

回答

0

代替:$article->populate($form->getData());

嘗試:$article->populate($request->getPost()->toArray());//bad idea

或:$article->populate($form->getData()->toArray());//good idea

起來: $form->getData()將返回實體,所以你必須實現toArray()功能;

public function toArray() 
{ 
    return array(
    'title' => $this->title, 
    //... 
); 
} 
+0

感謝@Notuser,仍然沒有工作,我認爲在填入功能 – Mohammadov

+0

上的var_dump $形式 - 問題>的getData() - >指定者(),並檢查該數組有相同的密鑰填入 – Skaza

+0

@Mohammadov對不起,我忘了你必須添加函數toArray()到你的實體 – Skaza