2012-12-24 38 views
3

我使用的是AnnotationForms,我改變了我的標準編輯動作,使用Annotation而不是標準形式。Zend Framework 2 - 註釋表單 - 綁定不起作用

$form->bind()之外的所有工作都沒有填寫值。表單域保持空白。

我檢查了我應該綁定的變量,它被設置並且看起來不錯。

這裏是我的行動:

$id = (int)$this->getEvent()->getRouteMatch()->getParam('id'); 
    if (!$id) { 
     return $this->redirect()->toRoute('album', array('action'=>'add')); 
    } 
    $album = $this->getEntityManager()->find('Album\Entity\Album', $id); 

    $builder = new AnnotationBuilder(); 
    $form = $builder->createForm(new \Album\Entity\Album()); 
    $form->add(new \MyVendor\Form\MyFieldset()); 
    $form->setBindOnValidate(false); 
    $form->bind($album); 

回答

2

好吧,這是一個很容易的!

訣竅是將您的對象轉換爲數組,並使用setData()而不是綁定。

我發現解決方案提示here

您仍然需要bind()來保存更改。如果將其保留,則不會發生錯誤,但它也不會保存。

$album = $this->getEntityManager()->find('Album\Entity\Album', $id); 
... 
$form->bind($album); 
$form->setData($album->getArrayCopy()); 
2

ClassMethods水化並在結合實際工作,但表單元素名稱需要匹配小寫下劃線格式(例如:對象屬性$moduleDisplayNameClassMethodsObjectProperty水化期望形式元素名稱module_display_name

Zend\Form\Annotation默認產生一個名爲moduleDisplayName表單元素因此提取對象的形式不起作用

解決方案1:。添加@Name物業註釋ClassMethods工作。

實施例:

/** 
* @Form\Name("module_display_name") 
protected $moduleDisplayName 

解決方案2:實例化ClassMethods水化器與可選參數underScoreSeperatedKeys設定爲false(默認爲true)或使用ClassMethods::setUnderScoreSeperatedKeys(false)。然後ClassMethods將表現得像任何其他水合物。

ObjectProperty水化器僅適用於聲明爲public的屬性。但是命名與ArraySerializable(即,屬性$moduleDisplayName,表單元素名稱moduleDisplayName)相同。

Reflection hydrator適用於所有屬性。命名與ArraySerializable相同。

結論:ClassMethods是四種水化器之一,它們需要不同的表單元素命名方案。不太酷。