Skip to content

Overriding Default FOSUserBundle Forms: form name unchanged #2924

@mohamedaymenkarmous

Description

@mohamedaymenkarmous

Symfony FOSUserBundle version 2.1.2:

Hello,

I'm using Symfony 4.3.1 and I followed this documentation to override the default FOSUserBundle and it looks great.

The only problem is about the form name (including form elements name) which is always "fos_user_registration_form"

Example:

<form name="fos_user_registration_form" method="post" action="/api/user_register" class="fos_user_registration_register">
   <div id="fos_user_registration_form">
      <div><label for="fos_user_registration_form_email" class="required">Email</label><input type="email" id="fos_user_registration_form_email" name="fos_user_registration_form[email]" required="required" /></div>
      <div><label for="fos_user_registration_form_username" class="required">Username</label><input type="text" id="fos_user_registration_form_username" name="fos_user_registration_form[username]" required="required" maxlength="180" pattern=".{2,}" /></div>
      <div><label for="fos_user_registration_form_plainPassword_first" class="required">Password</label><input type="password" id="fos_user_registration_form_plainPassword_first" name="fos_user_registration_form[plainPassword][first]" required="required" autocomplete="new-password" /></div>
      <div><label for="fos_user_registration_form_plainPassword_second" class="required">Repeat password</label><input type="password" id="fos_user_registration_form_plainPassword_second" name="fos_user_registration_form[plainPassword][second]" required="required" autocomplete="new-password" /></div>
   </div>
   <div>
      <input type="submit" value="Register" />
   </div>
</form> 

I searched how to override that form name "fos_user_registration_form" and I found two ways:

  • Overriding \AcmeBundle\Form\Type\RegistrationType::getBlockPrefix() method

  • Defining fos_user.registration.form.name attribute on the config/packages/fos_user.yaml file

But none of them works: form name is always "fos_user_registration_form".

So, I was forced to dig into the FOSUserBundle source code and I found something strange:

1) Inside Controller/RegistrationController.php file

When I replace this line:

$form = $this->formFactory->createForm();

With these lines

$form = $this->createForm(\AcmeBundle\Form\Type\RegistrationType::class, $user, array());

The form name changes to the configured name inside \AcmeBundle\Form\Type\RegistrationType::getBlockPrefix() which is logical (the behavior of Symfony).

But why FOSUserBundle is not using the Type class configured on fos_user.registration.form.type when the form is created ? (createForm method)

So i digged more inside another file

2) Inside Form/Factory/FormFactory.php file

Excuse my misunderstanding if I made a mistake but what I understood from the previous file that Form/Factory/FormFactory.php::createForm() is the method that creates the form but when I tried to debug inside this file (I mean inside the __construct() or the createForm() methods) using dump();exit(); I got nothing. Which means that createForm() is not called from this file.

3) Last test

I reproduced the first test and I removed \AcmeBundle\Form\Type\RegistrationType::getBlockPrefix() method so I get another form name (I think it's the default form name generated by Symfony) which is not "fos_user_registration_form".

Conclusion

So I think there is a problem with this line. I'm not an expert of Symfony. But this is what I can conclude after these tests. It's like the custom ReigstrationType is not used because like I showed on the first test: when we hardcode the Form Type in the form creation, everything works fine.

I don't want to keep that form name for some reason.

Can anyone help me to find the problem ?

My fos_user.yaml file content:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: AcmeBundle\Entity\User
    group:
        group_class:   AcmeBundle\Entity\Group
    service:
        user_manager: pugx_user_manager
    registration:
        form:
            type: AcmeBundle\Form\Type\RegistrationType
            name: user_acme_registration
            validation_groups: [Creation, Default]
        confirmation:
            enabled: false
    from_email:
        address: [email protected]
        sender_name: Admin

Thank you for your support

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions