title: Symfony2 Forms children and errors
body: <p>I'm building a site-wide search form and I'm having trouble understanding the Symfony2 Forms structure, regarding children forms and error handling.</p>
  
  <p>My code looks like this:</p>
  
  <p><strong>Form Type</strong></p>
  
  <pre><code>class SearchType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('q', 'search', array(
                  'label' =&gt; '',
                  'attr' =&gt; array(),
              ))
          ;
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'AppBundle\Entity\SearchQuery',
              'csrf_protection' =&gt; false,
          ));
      }
  
      public function getName()
      {
          return 'search_form';
      }
  }
  </code></pre>
  
  <p><strong>Entity (referred as <code>data_class</code> in the Form Type above)</strong></p>
  
  <pre><code>class SearchQuery
  {
      /**
       * @Assert\NotBlank()
       * @Assert\Type(type="string", message="form.search.query.validation.type")
       * @Assert\Length(
       *      min = 3,
       *      max = 50
       * )
       */
      protected $q;
  
      public function __construct($q = '')
      {
          $this-&gt;setQ($q);
      }
  
      public function __toString() {
          return $this-&gt;getQ();
      }
  
      public function getQ()
      {
          return $this-&gt;q;
      }
  
      public function setQ($query)
      {
          $this-&gt;q = $query;
      }
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>$searchForm = $this-&gt;createForm(new SearchType());
  $searchFormData = array('q' =&gt; $q);
  $searchForm-&gt;submit($searchFormData);
  
  if ($searchForm-&gt;isValid()) {
      [...]
  }
  else {
      // this returns an empty array regardless there are errors or not
      $errors = $searchForm-&gt;getErrors(); 
  
      // this returns errors when they are present
      $errorMessages = $searchForm-&gt;getErrorsAsString();
  }
  </code></pre>
  
  <p>You might notice on the second line in the controller code, that I'm submitting an array, not a <code>SearchQuery</code> object. I've tried that and it doesn't come out well, although I expected this to be the right approach.</p>
  
  <p>Digging in the Symfony documentation and questions here on StackOverflow helped me understand that <code>getErrors()</code> (<a href="https://code.google.com/r/capcom01-noob/source/browse/vendor/symfony/form/Symfony/Component/Form/Form.php?spec=svn55a6f14887ce1e6585f1a053b1bcf262f1c9e151&amp;r=55a6f14887ce1e6585f1a053b1bcf262f1c9e151#766" rel="nofollow">see it here</a>) never checks children forms and <code>getErrorsAsString()</code> (<a href="https://code.google.com/r/capcom01-noob/source/browse/vendor/symfony/form/Symfony/Component/Form/Form.php?spec=svn55a6f14887ce1e6585f1a053b1bcf262f1c9e151&amp;r=55a6f14887ce1e6585f1a053b1bcf262f1c9e151#780" rel="nofollow">see it here</a>) does. So I guessed that that's the reason behind the described behaviour and so it was, my form had a child form. </p>
  
  <p>My question is why is there a child form present and how to get rid of it? What am I doing wrong?</p>
  
  <p>Thanks!</p>