Use field of type Sonata in normal form class

I'm trying to insert my own sonata form field type on the first page, not in SonataAdmin, something like this:

 $form = $this->createFormBuilder($content)
            ->add('titleEs', 'text', array('required' => true, 'label' => 'label.title.spanish', 'attr' => array('class' => 'col-xs-12 form-control input-lg')))
            ->add('contentEs', 'ckeditor', array('required' => true,'label' => 'label.content.spanish', 'attr' => array('class' => 'col-xs-12')))
            ->add('titleEn', 'text', array('required' => true,'label' => 'label.title.english', 'attr' => array('class' => 'col-xs-12 form-control input-lg')))
            ->add('contentEn', 'ckeditor', array('required' => true, 'label' => 'label.content.english', 'attr' => array('class' => 'col-xs-12')))
            ->add('header', 'sonata_type_model', array('required' => true,'label' => 'label.content.headerImage'), array('link_parameters' => array('context' => 'content/front', 'size' => 'big')))
            //->add('coverImage', 'sonata_type_model_list', array('required' => true,'label' => 'label.content.coverImage'), array('link_parameters' => array('context' => 'content/front', 'size' => 'small')))
            //->add('sliderImage', 'sonata_type_model_list', array('required' => false,'label' => 'label.content.sliderImage'), array('link_parameters' => array('context' => 'content/slider', 'size' => 'normal')))
            ->getForm(); 

      

But when I express this, it throws an error:

Catchable Fatal Error: Argument 1 passed to Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList::__construct() must implement interface Sonata\AdminBundle\Model\ModelManagerInterface, null given

      

I can't figure out why or what Symfony is throwing me this error if the sonata form field types are services.

+3


source to share


2 answers


I just ran into the same problem as you, but since I first got to google I just post what I did to solve this problem. I am assuming you were creating the form dynamically in your controller. If not, you need to declare your class as a service and attach a service to it sonata.admin.manager.orm

.

    $form = $this->createFormBuilder()
        ->add('<name_of_field>', 'sonata_type_model', array(
            'multiple' => true,
            'class' => <className>::class,
            'property' => '<propertyName>',
            'model_manager' => $this->get('sonata.admin.manager.orm')
        ))

      



After that, it will display correctly for me, as in the admin context.

0


source


sonata_type_model needs to know which organization is associated with your area. If you define this in the admin class, sonata uses its own internal method to test the relationship. So if you define it outside of the admin then it is assigned null instead



-2


source







All Articles