title: How to map form data in Symfony to an object only when form is valid?
body: <p>Imagine example form in Symfony:</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder)
  {
      $builder
          -&gt;add('email', EmailType::class, [
              'constraints' =&gt; 
                  new NotBlank(),
                  new IsUnique(),
              ],
          ])
          -&gt;add('password', PasswordType::class, [
              'constraints' =&gt; 
                  new NotBlank(),
                  new IsStrongEnough(),
              ],
          ])
  }
  </code></pre>
  
  <p>Now when I submit the form and make sure it's valid, I'd like the <code>$form-&gt;getData()</code> to return my DTO called <code>CreateAccountCommand</code>:</p>
  
  <pre><code>final class CreateAccountCommand
  {
      private $email;
      private $password;
  
      public function __construct(string $email, string $password)
      {
          $this-&gt;email = $email;
          $this-&gt;password = $password;
      }
  
      public function getEmail(): string
      {
          return $this-&gt;email;
      }
  
      public function getPassword(): string
      {
          return $this-&gt;password;
      }
  }
  </code></pre>
  
  <p>Example controller:</p>
  
  <pre><code>$form = $this-&gt;formFactory-&gt;create(CreateAccountForm::class);
  $form-&gt;handleRequest($request);
  
  if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
      $this-&gt;commandBus-&gt;dispatch($form-&gt;getData());
  
      return new JsonResponse([]);
  }
  </code></pre>
  
  <p>I cannot use this class directly using the <code>data_class</code>, because the form obviously expects the model to have setters that allow null values. The form itself works perfectly well, so is the validation.</p>
  
  <p>I tried using the <a href="https://symfony.com/doc/master/bundles/SonataAdminBundle/cookbook/recipe_data_mapper.html" rel="nofollow noreferrer">Data mapper</a> method, but the <code>mapFormsToData</code> method is invoked, before the validation.</p>
  
  <p>Is this possible at all? Or am I supposed to get the data as array and create the object outside the form?</p>