visit our site

How to Enable Email Confirmation on FOSUserBundle Profile Edit

In Blog, PHP and Symfony

by Dragos Holban on August 24, 2015

We all know and use FOSUserBundle in our Symfony applications, so much it became kind of a standard. It provides everything you need for user management: login, registration, email confirmation and much more control over the access of the user in your application. But we found a thing missing from this awesome package: email confirmation after the initial email address has been changed through a profile edit. In the following lines we will show you how to extend the FOSUserBundle to implement this.

This post assumes you are familiar (even advanced) with the Symfony framework and FOSUserBundle.

To get started we will need a listener to be triggered when a profile edit has happened, FOSUserBundle fires two events that we are interested in: FOSUserEvents::PROFILE_EDIT_INITIALIZE and FOSUserEvents::PROFILE_EDIT_SUCCESS. The first one is triggered before the actual profile data is changed so we will use that to get a hold on the original email address. When the second event is fired, we will compare the initial email address with the current one and, if they are not the same, we will start the confirmation process:

Now, add this to your services.yml file and you’re good to go:

One last thing: you will probably want to change the email template that is sent to the user with the confirmation link. You can overwrite it by creating app/Resources/FOSUserBundle/views/Registration/email.txt.twig and put what you need in there (use the original one from vendor/friendsofsymfony/user-bundle/Resources/views/Registration/email.txt.twig to see how to get the confirmation link).

4 Replies to “How to Enable Email Confirmation on FOSUserBundle Profile Edit”

  1. Hi,

    Thx for this. I have an issue with the TokenStorage :

    Argument 5 passed to Brehat\UserBundle\EventListener\ProfileEditListener::__construct() must be an instance of Brehat\UserBundle\EventListener\TokenStorageInterface, instance of Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage given

    What did i wrong ?

Leave a Reply

Your email address will not be published. Required fields are marked *