Change User Disabled Exception Message In Symfony 2
The Problem
Symfony’s core message for when a user tries to log in, but is disabled is: “User account is disabled”. I guess that’s ok, but it’s really not very helpful to the user trying to log in. Initially, I set up an event listener in my bundle’s services.yml to a custom service that was intended to override the UserChecker.php core file (Symfony\Component\Security\Core\User
). After this incorrect and unsuccessful attempt, I realized that I could just change the translations yaml file in my bundle. DUH!
The Solution
So here’s how you do it. If you don’t already have a translations folder set up in your bundle under the Resources directory, do so. In my case I am using the FOSUserBundle. So, I copied the FOSUserBundle.en.yml file to my directory (overriding the FOSUserBundle translations for English). Your structure should look like this: Name/MyBundle/Resources/translations/FOSUserBundle.en.yml
In my case it has to be called “FOSUserBundle” so that the translation domain is correct. Below is the chunk of code I inserted into my yaml file.
# Security "User account is disabled.": Sorry, your account is pending review. An admin must enable and activate your account before you can log in.
The “# Security” is one of many comments used for separating the different categories of translations in the file. The “User account is disabled.” is the portion of text that I want to find, and the “Sorry, your account…” is the portion I want in return. For the project I am working on, an admin has to review and enable newly registered users before they can become active.
Hopefully this post will come in handy for you and you don’t end up wasting a couple hours like I did trying to figure out how to do something that is actually stupid easy. Check out Symfony’s docs about translations to learn more!
1 Comment
It works without the quotes, but you are correct, one should probably use quotes for best practice. I’ve updated the post to reflect this. Thanks!