tokenStorage = $tokenStorage; $this->encoderFactory = $encoderFactory; } /** * {@inheritdoc} */ public function validate($password, Constraint $constraint) { if (!$constraint instanceof UserPassword) { throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\UserPassword'); } $user = $this->tokenStorage->getToken()->getUser(); if (!$user instanceof UserInterface) { throw new ConstraintDefinitionException('The User object must implement the UserInterface interface.'); } // give username, it's used to hash the password $encoder = $this->encoderFactory->getEncoder($user); $encoder->setUsername($user->getUsername()); if (!$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) { $this->context->addViolation($constraint->message); } } }