title: Validating a password in Symfony2
body: <p>I'm trying to put together a change password feature in Symfony2. I have a "current password" field, a "new password" field and a "confirm new password" field, and the part I'm currently focusing on is validating the "current password" field.</p>
  
  <p>(By the way, I realize now that things like <code>FOSUserBundle</code> exist that would take care of a lot of these things for me, but I already built my authentication system based on the official Symfony documentation, and I don't have time right now to redo all my authentication code.)</p>
  
  <p>What I'm imagining/hoping I can do is create a validation callback that says something like this:</p>
  
  <pre><code>// Entity/User.php
  
  public function currentPasswordIsValid(ExecutionContext $context)
  {
    $currentPassword = $whatever; // whatever the user submitted as their current password
    $factory = $this-&gt;get('security.encoder_factory'); // Getting the factory this way doesn't work in this context.
    $encoder = $factory-&gt;getEncoder($this);
    $encryptedCurrentPassword = $encoder-&gt;encodePassword($this-&gt;getPassword(), $this-&gt;getSalt());
  
    if ($encyptedCurrentPassword != $this-&gt;getPassword() {
      $context-&gt;addViolation('Current password is not valid', array(), null);
    }
  }
  </code></pre>
  
  <p>As you can see in my comments, there are at least a couple reasons why the above code doesn't work. I would just post specific questions about those particular issues, but maybe I'm barking up the wrong tree altogether. That's why I'm asking the overall question.</p>
  
  <p>So, how can I validate a user's password?</p>