Skip to content

Instantly share code, notes, and snippets.

@kgust
Created March 31, 2024 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgust/6fcd1f44a256347de76d24af44359037 to your computer and use it in GitHub Desktop.
Save kgust/6fcd1f44a256347de76d24af44359037 to your computer and use it in GitHub Desktop.
This is an example Symfony form event listener.
<?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