Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7736447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:44:01+00:00 2026-06-01T07:44:01+00:00

I’m trying to put together a change password feature in Symfony2. I have a

  • 0

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.

(By the way, I realize now that things like FOSUserBundle 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.)

What I’m imagining/hoping I can do is create a validation callback that says something like this:

// Entity/User.php

public function currentPasswordIsValid(ExecutionContext $context)
{
  $currentPassword = $whatever; // whatever the user submitted as their current password
  $factory = $this->get('security.encoder_factory'); // Getting the factory this way doesn't work in this context.
  $encoder = $factory->getEncoder($this);
  $encryptedCurrentPassword = $encoder->encodePassword($this->getPassword(), $this->getSalt());

  if ($encyptedCurrentPassword != $this->getPassword() {
    $context->addViolation('Current password is not valid', array(), null);
  }
}

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.

So, how can I validate a user’s password?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:44:03+00:00Added an answer on June 1, 2026 at 7:44 am

    There’s a built-in constraint for that since Symfony 2.1.


    First, you should create a custom validation constraint. You can register the validator as a service and inject whatever you need in it.

    Second, since you probably don’t want to add a field for the current password to the User class just to stick the constraint to it, you could use what is called a form model. Essentially, you create a class in the Form\Model namespace that holds the current password field and a reference to the user object. You can stick your custom constraint to that password field then. Then you create your password change form type against this form model.

    Here’s an example of a constraint from one of my projects:

    <?php
    namespace Vendor\Bundle\AppBundle\Validator\Constraints\User;
    
    use Symfony\Component\Validator\Constraint;
    
    /**
     * @Annotation
     */
    class CurrentPassword extends Constraint
    {
        public $message = "Your current password is not valid";
    
        /**
         * @return string
         */
        public function validatedBy()
        {
            return 'user.validator.current_password';
        }
    }
    

    And its validator:

    <?php
    namespace Vendor\Bundle\AppBundle\Validator\Constraints\User;
    
    use Symfony\Component\Validator\ConstraintValidator;
    use Symfony\Component\Validator\Constraint;
    use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
    use Symfony\Component\Security\Core\SecurityContextInterface;
    use JMS\DiExtraBundle\Annotation\Validator;
    use JMS\DiExtraBundle\Annotation\InjectParams;
    use JMS\DiExtraBundle\Annotation\Inject;
    
    /**
     * @Validator("user.validator.current_password")
     */
    class CurrentPasswordValidator extends ConstraintValidator
    {
        /**
         * @var EncoderFactoryInterface
         */
        private $encoderFactory;
    
        /**
         * @var SecurityContextInterface
         */
        private $securityContext;
    
        /**
         * @InjectParams({
         *     "encoderFactory"  = @Inject("security.encoder_factory"),
         *     "securityContext" = @Inject("security.context")
         * })
         *
         * @param EncoderFactoryInterface  $encoderFactory
         * @param SecurityContextInterface $securityContext
         */
        public function __construct(EncoderFactoryInterface  $encoderFactory,
                                    SecurityContextInterface $securityContext)
        {
            $this->encoderFactory  = $encoderFactory;
            $this->securityContext = $securityContext;
        }
    
        /**
         * @param string     $currentPassword
         * @param Constraint $constraint
         * @return boolean
         */
        public function isValid($currentPassword, Constraint $constraint)
        {
            $currentUser = $this->securityContext->getToken()->getUser();
            $encoder = $this->encoderFactory->getEncoder($currentUser);
            $isValid = $encoder->isPasswordValid(
                $currentUser->getPassword(), $currentPassword, null
            );
    
            if (!$isValid) {
                $this->setMessage($constraint->message);
                return false;
            }
    
            return true;
        }
    }
    

    I use my Blofwish password encoder bundle, so I don’t pass salt as the third argument to the $encoder->isPasswordValid() method, but I think you’ll be able to adapt this example to your needs yourself.

    Also, I’m using JMSDiExtraBundle to simplify development, but you can of course use the classical service container configuration way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.