Created
March 31, 2024 02:20
-
-
Save kgust/6fcd1f44a256347de76d24af44359037 to your computer and use it in GitHub Desktop.
This is an example Symfony form event listener.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$builder->addEventListener(FormEvents::POST_SUBMIT, function ($event) { | |
$form = $event->getForm(); | |
$oldPassword = $form->get('old_password')->getData(); | |
$newPassword = $form->get('password')->get('first')->getData(); | |
$user = $this->security->getUser(); | |
if (!$this->passwordEncoder->isPasswordValid($user, $oldPassword)) { | |
$form->get('old_password')->addError(new FormError('The old password is incorrect.')); | |
} | |
if ($this->passwordEncoder->isPasswordValid($user, $newPassword)) { | |
$form->get('password')->addError(new FormError('The new password should be different from the old password.')); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment