title: Symfony2 - using entity value as default when form is submitted with empty fields
body: <p>I'm building a web application in Symfony 2 where I have a User entity class for holding registered users. </p>
  
  <p>Every user should have at least a username and a password of course, hence, I use the <code>@Assert\NotBlank</code> validation rule for both fields in the entity. The username is static, however, being able to change the password is desirable. </p>
  
  <p>Thus, I'm building a form where signed in users can change their password (among other things). This form utilizes the repeated field type in Symfony for password confirmation purposes. </p>
  
  <p>Now, in most cases I guess users are not looking to change their password, but rather update other fields in their profile. Therefore, I would want it to be possible for them to leave the password field blank. However, that will interfere with the <code>NotBlank</code> validation rule. </p>
  
  <p>Removing the <code>NotBlank</code> validation rule is of course an option. Although, hardly the solution I'm looking for since that incures the problem of users ending up with a blank password. </p>
  
  <p>Finally, As I understand it the symfony Request object autofills the form data back into the User entity on form submission. </p>
  
  <p>Now, my question is: Is there any way to, upon empty password-field, make Symfony refill the User entity with the existing password from the database rather than a blank value? This would allow validation to follow through and won't cause any undesired changes to the password upon saving. </p>
  
  <p>This is the code for the password field in the entity</p>
  
  <pre><code>/**
   * @var string
   * 
   * @ORM\Column(name="password",type="string",length=255) 
   * @Assert\NotBlank()
   * @Assert\Type(type="string")
   * @Assert\Length(max="255")
   * @Assert\NotEqualTo(value="password")
   */
  private $password;
  </code></pre>
  
  <p>This is the code for generating the form</p>
  
  <pre><code>private function initForm($user) {
    return $this-&gt;createFormBuilder($user)
             -&gt;add('name', 'text')
             -&gt;add('mail', 'email')
             -&gt;add('password', 'repeated', [
                'type' =&gt; 'password',
                'invalid_message' =&gt; 'The password fields must match.',
                'first_options' =&gt; ['label' =&gt; false],
                'second_options' =&gt; ['label' =&gt; false] //set to false to hide label in view
             ])
             -&gt;add('save', 'submit')-&gt;getForm();
  }
  </code></pre>
  
  <p>This is the Action-code for the <code>/profile/edit</code> page:</p>
  
  <pre><code>/**
   *  @ParamConverter("user",class="MyBundle:User")
   */
  public function editAction(User $user, Request $request) {
  
     $form = $this-&gt;initForm($user);
     $form-&gt;handleRequest($request);
  
     if ($form-&gt;isValid()) {
        //update to database and redirect user back to profile_view
        $this-&gt;getDoctrine()-&gt;getManager()-&gt;flush();
        return $this-&gt;redirect($this-&gt;generateUrl('profile_view'));
     }
  
     return $this-&gt;render('MyBundle:User:edit.html.twig',
                          array('form' =&gt; $form-&gt;createView()));
  }
  </code></pre>
  
  <p>Thanks in advance for any help!</p>
  
  <h1>EDIT</h1>
  
  <p>I did try the "obvious" approach, i.e. temporarilly storing the original password in a variable and "putting" it back if it was blank after the form was submitted:</p>
  
  <pre><code>/**
   * @ParamConverter("user",
                     class="MyBundle:User")
   */
  public function editAction(User $user, Request $request) {
     $password = $user-&gt;getPassword();
  
     $form = $this-&gt;initForm($user)-&gt;getForm();
     $form-&gt;handleRequest($request);
  
     if ( $user-&gt;getPassword() == NULL ) {
         $user-&gt;setPassword($password);
         $form = $this-&gt;initForm($user)-&gt;getForm(); //this is the problem
  
     }
  
     if ( $form-&gt;isValid() ) {
         //update to database and redirect user back to profile view
         $this-&gt;getDoctrine()-&gt;getManager()-&gt;flush();
         return $this-&gt;redirect($this-&gt;generateUrl('profile_view'));
     }
     return $this-&gt;render('MyBundle:User:edit.html.twig',
                          array('form' =&gt; $form-&gt;createView()));
  }
  </code></pre>
  
  <p>However, this did not work for apparent reasons. When the form is reinitialised within the <code>if</code> statement, Symfony finds the form has never been submitted in the first place, i.e. no validation is performed. And adding the <code>$form-&gt;handleRequest($request)</code> leaves us back with the first problem where the password field might be blank.</p>
  
  <p>What I do find peculiar though. Symfony validates the Entity and not the form. But removing the problem row <code>$form-&gt;initForm($user)-&gt;getForm()</code> still didn't work, although the entity should be valid.</p>