title: Symfony2 dynamic forms mongodb
body: <p>Following the example as documented in the following <a href="http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html" rel="nofollow">http://symfony.com/doc/current/cookbook/form/dynamic_form_generation.html</a></p>
  
  <p>I need to populate subcategory field according to the category selection.
  I don't understand why this error at /article/create: </p>
  
  <pre><code>FatalErrorException: Error: Call to a member function getSubCategories() on a non-object
  </code></pre>
  
  <p>the Form :
      
  
  <pre><code>namespace Cc\HitoBundle\Form\Type;
  
  …
  
  class ArticleType extends AbstractType
  {
      private $registrationArticleListener;
  
      public function __construct(RegistrationArticleListener $registrationArticleListener)
      {
          $this-&gt;registrationArticleListener = $registrationArticleListener;
      }
  
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              …
  
              -&gt;add('category', 'document', array(
                  'label' =&gt; 'Category',
                  'class' =&gt; 'Cc\HitoBundle\Document\Category',
                  'property' =&gt; 'name',
              ));
  
  
          $builder-&gt;addEventSubscriber($this-&gt;registrationArticleListener);
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Cc\HitoBundle\Document\Article'
          ));
      }
  
      public function getName()
      {
          return 'article';
      }
  }
  </code></pre>
  
  <p>I don't understand what is the <strong>AddNameFieldSubscriber()</strong> function in the cookbook</p>
  
  <p>The Listener:
      
  
  <pre><code>namespace Cc\HitoBundle\Form\EventListener;
  
  …
  
  class RegistrationArticleListener implements EventSubscriberInterface
  {
      /**
       * @var FormFactoryInterface
       */
      private $factory;
  
      /**
       * @var DocumentManager
       */
      private $dm;
  
      /**
       * @param factory FormFactoryInterface
       */
      public function __construct(FormFactoryInterface $factory, $mongo)
      {
          $this-&gt;factory = $factory;
          $this-&gt;dm = $mongo-&gt;getManager();
      }
  
      public static function getSubscribedEvents()
      {
          return array(
              FormEvents::PRE_BIND =&gt; 'preBind',
              FormEvents::PRE_SET_DATA =&gt; 'preSetData',
          );
      }
  
      /**
       * @param event FormEvent
       */
      public function preSetData(FormEvent $event)
      {
          $agent = $event-&gt;getData();
  
          if (null === $article) {
              return;
          }
  
          $form = $event-&gt;getForm();
          $subCategories = $article-&gt;getCategory()-&gt;getSubCategories();
  
          $this-&gt;customizeForm($form, $subCategories);
      }
  
      public function preBind(FormEvent $event)
      {
          $data = $event-&gt;getData();
          $id = $data['event'];
          $agent = $this-&gt;dm
              -&gt;getRepository('CcHitoBundle:Article')
              -&gt;find($id);
  
          if ($article === null) {
              $msg = 'The event %s could not be found for you registration';
              throw new \Exception(sprintf($msg, $id));
          }
          $form = $event-&gt;getForm();
          $subCategories = $article-&gt;getCategory()-&gt;getSubCategories();
  
          $this-&gt;customizeForm($form, $subCategories);
      }
  
      protected function customizeForm($form, $subCategories)
      {
  
      }
  }
  </code></pre>
  
  <p>And I don't understand what I have to put in customizeForm function. Please help me !! =)</p>