I found a solution, It's working but I still have a small issue : when i submit my new password, if i try to put the same password as the previous one, my form is not valid (that's great !), but the error message is not shown by my twig whereas I'm using :
{{ form_widget(edit_form) }}
Any idea why?
Anyway here is my solution :
I added a validator on the class level to verify the password. To do so, i added a validator on my User class :
// \src\Acme\UserBundle\Resources\config\validation.xml
<class name="Acme\UserBundle\Entity\User">
<constraint name="Acme\UserBundle\Validator\Constraints\IsDifferentPassword">
<option name="message">Password has to be different from the previous one</option>
</constraint>
<property name="plainPassword">
<constraint name="NotBlank">
<option name="message">fos_user.password.blank</option>
</constraint>
</property>
</class>
And i created a validator on the class level :
// \src\Acme\UserBundle\Validator\Constraints\IsDifferentPassword.php
namespace Acme\UserBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class IsDifferentPassword extends Constraint
{
public $message = 'Password has to be different from the previous one';
public function getTargets()
{
return Constraint::CLASS_CONSTRAINT;
}
}
And :
// \src\Acme\UserBundle\Validator\Constraints\IsDifferentPasswordValidator.php
namespace Acme\UserBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
class IsDifferentPasswordValidator extends ConstraintValidator
{
public function isValid($value, Constraint $constraint)
{
$encoder = new MessageDigestPasswordEncoder();
$password = $encoder->encodePassword($value->getPlainPassword(), $value->getSalt());
$oldPassword = $value->getPassword();
if($password == $oldPassword){
return false;
}
return true;
}
}