【问题标题】:using sonata_type_collection against a custom form type instead of a property/relationship with another entity对自定义表单类型使用 sonata_type_collection 而不是与另一个实体的属性/关系
【发布时间】:2012-06-11 12:29:11
【问题描述】:

是否可以将sonata_type_collection 用于自定义表单类型而不是与另一个实体的属性/关系?

类似这样的:

$formMapper->add('foo', 'sonata_type_collection',
    array('type' => new \myVendor\myBundleBundle\Form\Type\BlockType() 
));

这会引发以下错误

The current field `contingut` is not linked to an admin. Please create one for the target entity : ``

编辑:

这样的事情可以解决问题:

$formMapper->add('contingut', 'collection', array(                                 
    'type' => new \myVendor\myBundleBundle\Form\Type\Block1Type(),
    'allow_add' => true,
    'allow_delete' => true,
    'by_reference' => false 
));

【问题讨论】:

  • 从来没有人将基于模型的表单与奏鸣曲结合使用?我的意思是,每个人都曾经使用过 SOnata,仅用于基于实体的表单吗?提前致谢!
  • 到目前为止,我没有使用自定义表单类型,而是覆盖了 form_admin_fields.html 模板,然后捕获集合名称,例如foo 并包含一个 _foo.html.twig 模板,该模板基于 edit_orm_one_to_many.html.twig,例如在使用 OneToMany 时。然后我在 twig 中根据需要定义表单和模板。但是使用自定义类型听起来很有趣! @user846226 对你有用吗?

标签: symfony symfony-forms symfony-sonata sonata-admin


【解决方案1】:

除了示例中的自定义类型,您还可以使用本机实体类型。

$formMapper->add('cars',
                            'collection',
                            array(
                                'type' => 'entity',
                                'allow_add' => true,
                                'allow_delete' => true,
                                'by_reference' => false,
                                'label' => false,
                                'options' => array(
                                    'class' => 'YourBundle:Car',
                                    'property' => 'name',
                                    'label' => false
                                )
                            )
                        )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 2012-07-15
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多