在自定义类型中使用 Sonata_type_collection [英] Use sonata_type_collection inside custom type

查看:29
本文介绍了在自定义类型中使用 Sonata_type_collection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是将 Sonata_type_collection 添加到我的自定义 formType.正常的方法是将 Sonata_collection_type 添加到 AdminClass 中的 $formMaper 中,例如:

What I want to do is add sonata_type_collection to my custom formType. Normal way is add sonata_collection_type to $formMaper inside AdminClass like:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper->add('elements, 'sonata_type_collection', array(
             'some_options' => 'options'
          ))
 }

它工作完美,但我有我的自定义表单类型,当我定义它时:

It work perfect, but i have my custom form type, and when i defined it like:

public function buildForm(FormBuilderInterface $builder, array $options)
{

     $formMapper->add('elements, 'sonata_type_collection', array(
             'some_options' => 'options'
          ))
 }

它不起作用(它只显示已归档的标签).问题是模板错误,所以我尝试设置 formAdminTemplate我是通过视图中的设置模板制作的

It doesn't work (it appear only label of filed). Problem is wrong template, so I tried to set formAdminTemplate I made it by set template in view

   {% form_theme formElement 'SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig' %}

问题是这个'formTheme' 中的sonata_admin 变量.这个变量在我的表单中不存在.

Problem is sonata_admin variable inside this 'formTheme'. This variable doesn't exist in my form.

当然,我的表单类型与 admin 类有关,但我不知道如何将这种关系告诉 symfony

Of course my form type is related to admin class but i don't know how could I I tell symfony about this relation

推荐答案

我找到了解决方案.我使用 admin 类定义了表单,而不是使用我的自定义类型.我在管理员之外需要这个表格,所以这有点困难.

I find solution. Instead using my custom type, I defined form using admin class. I need this form outside admin so it was little difficult.

首先在我的控制器中,我从服务中获得管理类.在管理类中,我覆盖了 3 个用于创建表单的方法

First of all in my controller i get admin class from service. Inside admin class I override 3 methods which are use to create form

 public function getFormBuilder()

 public function defineFormBuilder(FormBuilder $formBuilder)

 public function buildForm()

然后我不得不通过奏鸣曲管理方式保存我的实体.使用 create 方法代替 handleRequest.

then i had to save my entity by sonata admin way. using create method instead handleRequest.

这篇关于在自定义类型中使用 Sonata_type_collection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆