title: Symfony 3 Form component - my entity constructor has arguments
body: <p>I have an entity called User and the form to register a new user. The problem I am having is that my entity has arguments in its constructor, so I can not instantiate a new empty User that has no data in it and pass it to the form builder in the controller. When I use default options on my form I then get the error message</p>
  
  <blockquote>
    <p>Warning: Missing argument 1 for User\User::__construct(), called in /mnt/project/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/Type/FormType.php on line 136 and defined</p>
  </blockquote>
  
  <p>I do not want to allow User object be constructed without mandatory things like email or name. What's the solution here?</p>
  
  <p>The form:</p>
  
  <pre><code>class UserRegisterForm extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('email', TextType::class)
              -&gt;add('password', PasswordType::class)
              -&gt;add('firstName', TextType::class)
              -&gt;add('lastName', TextType::class)
              -&gt;add('save', SubmitType::class);
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; User::class
          ));
      }
  }
  </code></pre>
  
  <p>My entity:</p>
  
  <pre><code>/**
   * @ORM\Entity
   */
  class User implements UserInterface
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
  
      /**
       * @ORM\Column(type="string")
       * @Assert\Email(
       *     message = "The email '{{ value }}' is not a valid email.",
       *     checkMX = true
       * )
       */
      protected $email;
  
      /**
       * @ORM\Column(type="string")
       * @Assert\NotBlank()
       * @Assert\Length(min=3)
       */
      protected $password;
  
      /**
       * @ORM\Column(type="string")
       * @Assert\NotBlank()
       */
      protected $firstName;
  
      /**
       * @ORM\Column(type="string")
       * @Assert\NotBlank()
       */
      protected $lastName;
  
      public function __construct($email, $firstName, $lastName)
      {
          $this-&gt;email = $email;
          $this-&gt;firstName = $firstName;
          $this-&gt;lastName = $lastName;
      }
  }
  </code></pre>
  
  <p>And the controller</p>
  
  <pre><code>public function registerView(Request $request)
  {
      $form = $this-&gt;formFactory
          -&gt;createBuilder(UserRegisterForm::class)
          -&gt;getForm();
  
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
          die("Valid form, the user entity is going to be saved here");
      }
  
      return $this-&gt;render("User/register.html.twig", ["form" =&gt; $form-&gt;createView()]);
  }
  </code></pre>
  
  <p><strong>Update</strong>
  Found this - <a href="https://stackoverflow.com/questions/17495846/symfony2-forms-how-to-use-parametrized-constructors-in-form-builders">Symfony2 Forms - How to use parametrized constructors in form builders</a>. it does not solve the problem though because I do not want to pass any dummy data to the constructor. Not passing the object to the form builder causes it to automatically bypass the whole validation.</p>