Use sonata_type_collection inside custom type

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 08:21:22

问题


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'
          ))
 }

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' %}

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

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


回答1:


You need an admin class for your collection child :

$formMapper->add('customizations', 'sonata_type_collection',
    array(
        'required'      => true,
        'type_options'  => array('delete' => true),
        'by_reference'  => false,
        'mapped'        => true
    ),
    array(
        'edit'          => 'inline',
        'inline'        => 'table',
        'sortable'      => 'position',
        'targetEntity'  => '/path/to/Entity/Customization',
        'admin_code'    => 'my.service.customization_admin'
    )
);



回答2:


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.

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()

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



来源:https://stackoverflow.com/questions/27982110/use-sonata-type-collection-inside-custom-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!