title: Symfony Dynamically Adding Entry to Form Leads to TypeError
body: <p>In my form, I have a collection of forms embedded. The idea is to use jQuery to add or remove an entry (from the collection). The jQuery part works fine. I know for a fact that my entities are fine as well, because I used EasyAdminBundle before to realise this functionality and that was no problem. </p>
  
  <p>The problem is: When I add an entry via jQuery and submit the form, I get the following error:</p>
  
  <p><code>Type error: Return value of App\Entity\Answer::getIsCorrect() must be of the type boolean, null returned</code></p>
  
  <p>This is the parent form:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Form\Type;
  
  use App\Entity\Question;
  use App\Form\DataTransformer\TagTransformer;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  
  class QuestionType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('answers', CollectionType::class, array(
                      'entry_type' =&gt; AnswerType::class,
                      'allow_add' =&gt; true,
                      'allow_delete' =&gt; true,
                      'delete_empty' =&gt; true,
                      'entry_options' =&gt; ['label' =&gt; false],
                      'prototype' =&gt; true,
                      'label' =&gt; false,
                      'by_reference' =&gt; false,
                  )
              )
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(
              array(
                  'data_class' =&gt; Question::class
              )
          );
      }
  }
  </code></pre>
  
  <p>This is the collection form:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Form\Type;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  use Symfony\Component\Form\Extension\Core\Type\TextType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  use App\Entity\Answer;
  use Symfony\Component\Validator\Constraints\Choice;
  
  class AnswerType extends AbstractType
  {
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add(
                  'isCorrect',
                  CheckboxType::class,
                  ['required' =&gt; false]
              )
              -&gt;add(
                  'text',
                   TextType::class,
                  ['label' =&gt; false, 'required' =&gt; false]
              );
      }
  
      /**
       * @param OptionsResolver $resolver
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; Answer::class
          ));
      }
  }
  </code></pre>
  
  <p>Here are some facts:</p>
  
  <ul>
  <li>Symfony 3.3 used</li>
  <li>Entities work fine</li>
  <li>jQuery works fine</li>
  <li>Forms with fixtures can be submitted fine</li>
  <li>The whole setup is largely inspired by <a href="http://symfony.com/doc/current/form/form_collections.html" rel="nofollow noreferrer">this tutorial</a></li>
  </ul>
  
  <p>I read that there could be a problem when <code>allow_add</code> is not true. But that is explicitly set. Removing entries works fine. <code>delete_empty</code> is ignored as well.</p>
  
  <p>My first thought was that there is a problem because the form fields are not duplicated correctly by jQuery, e.g. wrong name attribute used. Especially checkboxes can sometimes be problematic. But even if I only use a text field, the issue persists. As an example:</p>
  
  <p>Generated with Twig:</p>
  
  <pre><code>&lt;input type="checkbox" id="question_answers_2_isCorrect" name="question[answers][2][isCorrect]" value="1"&gt;
  </code></pre>
  
  <p>Result from jQuery:</p>
  
  <pre><code>&lt;input type="checkbox" id="question_answers_3_isCorrect" name="question[answers][3][isCorrect]" value="1"&gt;
  </code></pre>
  
  <p>Now, here comes the kicker: In the Stack Trace, you can clearly see that the form has the data in the first place, but it gets lost. </p>
  
  <pre><code>Symfony\Component\Debug\Exception\FatalThrowableError:
  Type error: Return value of App\Entity\Answer::getIsCorrect() must be of the type boolean, null returned
  
    at src/Entity/Answer.php:82
    at App\Entity\Answer-&gt;getIsCorrect()
       (vendor/symfony/property-access/PropertyAccessor.php:487)
    at Symfony\Component\PropertyAccess\PropertyAccessor-&gt;readProperty(array(object(Answer)), 'isCorrect')
       (vendor/symfony/property-access/PropertyAccessor.php:410)
    at Symfony\Component\PropertyAccess\PropertyAccessor-&gt;readPropertiesUntil(array(object(Answer)), object(PropertyPath), 1, true)
       (vendor/symfony/property-access/PropertyAccessor.php:179)
    at Symfony\Component\PropertyAccess\PropertyAccessor-&gt;getValue(object(Answer), object(PropertyPath))
       (vendor/symfony/form/Extension/Core/DataMapper/PropertyPathMapper.php:92)
    at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper-&gt;mapFormsToData(object(RecursiveIteratorIterator), object(Answer))
       (vendor/symfony/form/Form.php:630)
  
  // Here the data is suddenly lost
  
    at Symfony\Component\Form\Form-&gt;submit(array(), true)
       (vendor/symfony/form/Form.php:574)
    at Symfony\Component\Form\Form-&gt;submit(array(array('text' =&gt; 'this gets lost')), true)
       (vendor/symfony/form/Form.php:574)
  
  // Here the data comes in, now see above ↑
  
  at Symfony\Component\Form\Form-&gt;submit(array('answers' =&gt; array(array('isCorrect' =&gt; '1', 'text' =&gt; 'foo'), array('text' =&gt; 'bar'), array('text' =&gt; 'baz'), array('text' =&gt; 'this gets lost'))), true)
       (vendor/symfony/form/Extension/HttpFoundation/HttpFoundationRequestHandler.php:113)
    at Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler-&gt;handleRequest(object(Form), object(Request))
       (vendor/symfony/form/Form.php:500)
    at Symfony\Component\Form\Form-&gt;handleRequest(object(Request))
       (src/Controller/QuestionsController.php:79)
  </code></pre>
  
  <p>Here's the entity <code>Answer</code>:</p>
  
  <p>
  
  <pre><code>class Answer
  {
      /**
       * @var
       * @ORM\Column(type="string")
       */
      protected $text;
  
      /**
       * @var
       * @ORM\Column(type="boolean")
       */
      protected $isCorrect;
  
      /**
       * @var
       * @ORM\ManyToOne(targetEntity="Question", inversedBy="answers", cascade={"persist"})
       */
      protected $question;
  
      /**
       * @return mixed
       */
      public function getText() : string
      {
          return $this-&gt;text;
      }
  
      /**
       * @param mixed $text
       */
      public function setText(string $text) : void
      {
          $this-&gt;text = $text;
      }
  
      /**
       * @return mixed
       */
      public function getIsCorrect() : bool
      {
          return $this-&gt;isCorrect;
      }
  
      /**
       * @param bool $isCorrect
       */
      public function setIsCorrect(bool $isCorrect) : void
      {
          $this-&gt;isCorrect = $isCorrect;
      }
  }
  </code></pre>