1

I'm trying to remove the username field from the FOSUserBundle registration form as described in step 2 in this answer.

This is the FormType class I've created in order to override the default:

<?php

// src/UserBundle/Form/Type/RegistrationFormType.php

namespace UserBundle\Form\Type;

use FOS\UserBundle\Form\Type\RegistrationFormType as BaseFormType;

class RegistrationFormType extends BaseFormType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder->remove('username');
    }
}

The username fields still shows up in the form, however. What am I missing?

Community
  • 1
  • 1
Yngve Høiseth
  • 570
  • 6
  • 26

1 Answers1

1

You're not overriding the form-service in the bundle's configuration.

That's why FOSUserBundle doesn't use your form type.

# register your form-type as a service ...
services:
    my_custom_user_registration_form:
        class:  "UserBundle\Form\Type\RegistrationFormType"

# ... then tell FOSUserBundle to use this form-type service instead of the default
fos_user:
    ...
    registration:
        form:
            type: my_custom_user_registration_form
Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
  • Thanks. You pointed me in the right direction. I needed some additional details, though, which I found in [this guide](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/1.3.x/Resources/doc/overriding_forms.md) in the FOSUserBundle docs. I should have looked more thoroughly in the docs before posting this question. I also needed to add the following line to `imports` in `app/config/config.yml`: `- { resource: "@UserBundle/Resources/config/services.yml" }` In addition, I needed to add `use Symfony\Component\Form\FormBuilderInterface;` to `RegistrationFormType.php`. – Yngve Høiseth Jul 07 '15 at 20:48