3

我正在使用AnnotationForms并且我将教程中的标准编辑操作更改为使用注释而不是标准表单。

一切正常,除了$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);
4

2 回答 2

2

好吧,这很容易!

诀窍是将您的对象转换为数组并使用setData()而不是绑定。

我在这里找到了解决方案提示。

您仍然需要bind()保存更改。如果您将其忽略,则不会发生错误,但也不会保存它。

 $album = $this->getEntityManager()->find('Album\Entity\Album', $id);
 ...
 $form->bind($album);
 $form->setData($album->getArrayCopy());
于 2012-12-24T09:28:50.173 回答
2

hydratorClassMethods实际上对 bind 起作用,但表单元素名称需要匹配小写下划线格式(例如: object property$moduleDisplayNameClassMethodshydratorObjectProperty期望表单元素 Name module_display_name

Zend\Form\Annotation默认情况下会生成一个名为 的表单元素moduleDisplayName。因此,将对象提取到表单中不起作用。

解决方案 1:将@Name注释添加到属性以ClassMethods使其工作。

例子:

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

解决方案 2:将ClassMethodshydrator 实例化,将可选参数underScoreSeperatedKeys设置为false(默认为true)或使用ClassMethods::setUnderScoreSeperatedKeys(false). 然后ClassMethods将像任何其他保湿剂一样工作。

ObjectPropertyhydrator 仅适用于声明的属性public。但命名与with相同ArraySerializable(即property $moduleDisplayName,form element Name moduleDisplayName)。

Reflection保湿剂适用于所有属性。命名与 with 相同ArraySerializable

结论:ClassMethods是期望不同的表单元素命名方案的四个水合器之一。不太酷。

于 2013-10-27T20:52:09.170 回答