This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

Quick Guide to Overriding the Default Registration Form in FOSUserBundle

Updated on  28th March

4 Min Read
Reading Time: 4 minutes

In a previous tutorial on how to implement FOSUserBundle in Symfony 3.1, I described the installation and configuration of the bundle and how you could manage registration and the login system.  

The default registration form simply contains four fields: Email, Username, Password and Repeat Password. As you could see, this is a very simple form and is not really suitable for real world projects.  

Overriding the Default Registration Form in FOSUserBundle

In this article, I will add more fields to the default FOSUserBundle registration form. I will also demonstrate how you could override the default form. I am assuming you have already installed Symfony and FOSUserBundle.

Add Property and Validators

When implementing the FOSUserBundle, the user entity only contains User ID for the records.

I want to add two more fields, First Name and Last Name to the form. For this, go to src/AppBundle/Entity and open User.php. Add two more properties, just below the protected $id;

Save the file. The next step is to generate the methods for new properties.

Open your preferred SSH terminal (I am using PuTTY).

Generate Entity Methods and Fields

Move to the project’s folder and run the following Doctrine command to generate methods for new properties.

This command will create the required methods for the first name and the last name. At this point, the full User entity looks like this:

The entity is all set. Now, I need to populate the fields in the database. For this, I will run the schema update command.

The above command updates the database and add first_name and last_name columns. After the update, the database fields will look like:

Create Registration Form Type

Next, it’s time to create a custom form type in Symfony. This form type integrates both custom and default input fields.  

I need to create a form type in the bundle. This new class inherits from the base FOSUserBundle fos_user_registration type using the form type hierarchy and then adds in the custom fields.

Create a Form folder in the src/AppBundle and create a file RegistrationType.php in it. Add the following code in this file.

Declare Form Type Service

After creating the Custom Form type, add a tag and name it service.yml. The tag contains the name whose value is form.type and the alias which contains the value returned by getName() method.

Update Config with Custom Form Type

Finally, I will update the config.yml with custom Form type, so that the FOSUserBundle use this command instead of the default command.

Let’s test the above command by registering a user. Go to the application URL and append/register with it: http://phpstack-21306-71265-234521.cloudwaysapps.com/fos/web/register/

The user should be successfully registered when you click the Register button.

Now, check the database for the custom values in the First and Last Name column.

Related: Create A Contact Form In Symfony

Final Words

FOSUserBundle provides comprehensive user management. However, the best thing is that you could easily add custom functionalities and override default registration form In FOSUserBundle without any issues. Similarly, you can also override the default templates of FOSUserBundle and create a more friendly UI for the login forms.

If you have any query or questions, feel free to comment below.

Share your opinion in the comment section. COMMENT NOW

Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at shahroze.nawaz@cloudways.com

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.