Calling $ builder-> getData () from within a nested form always returns NULL

I am trying to get data stored in a nested form, but when calling $builder->getData() I always get NULL.

Does anyone know how to get data inside a nested form?

Here is ParentFormType.php:

  class ParentFormType extends AbstractType
     {
         public function buildForm (FormBuilderInterface $ builder, array $ options)
         {
             $ builder-> add ('files', 'collection', array (
                 'type' => new FileType (),
                 'allow_add' => true,
                 'allow_delete' => true,
                 'prototype' => true,
                 'by_reference' => false
             );

FileType.php

  class FileType extends AbstractType
     {
         public function buildForm (FormBuilderInterface $ builder, array $ options)
         {

 // Each one of bellow calls returns NULL
 print_r ($ builder-> getData ());
 print_r ($ builder-> getForm () -> getData ());
 die ();
             $ builder-> add ('file', 'file', array (
                                                     'required' => false,
                                                     'file_path' => 'file',
                                                     'label' => 'Select a file to be uploaded',
                                                     'constraints' => array (
                                                         new File (
                                                             array (
                                                                 'maxSize' => '1024k',        
                                                             )
                                                         )
                                                     )
                 )
             );
         }

         public function setDefaultOptions (\ Symfony \ Component \ OptionsResolver \ OptionsResolverInterface $ resolver)
         {
             return $ resolver-> setDefaults (array ());
         }

         public function getName ()
         {
             return 'FileType';
         }
     }

Thanks!

+7
symfony symfony-forms
source share
3 answers

To get a form object, you need to use FormEvents :: POST_SET_DATA:

  $builder->addEventListener(FormEvents::POST_SET_DATA, function ($event) { $builder = $event->getForm(); // The FormBuilder $entity = $event->getData(); // The Form Object // Do whatever you want here! }); 
+5
source share

This is a (very annoying ..) known issue:

https://github.com/symfony/symfony/issues/5694

Since it works great for a simple form, but not for a composite form. From the documentation (see http://symfony.com/doc/master/form/dynamic_form_modification.html ), you should do:

 $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { $product = $event->getData(); $form = $event->getForm(); // check if the Product object is "new" // If no data is passed to the form, the data is "null". // This should be considered a new "Product" if (!$product || null === $product->getId()) { $form->add('name', TextType::class); } }); 
+2
source share

The form is created before data binding (i.e. related data is not available when AbstractType::buildForm() )

If you want to dynamically create your form based on related data, you will need to use events

http://symfony.com/doc/2.3/cookbook/form/dynamic_form_modification.html

-one
source share

All Articles