Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding Default FOSUserBundle Forms: form name unchanged #2924

Open
mohamedaymenkarmous opened this issue Aug 6, 2019 · 0 comments
Open

Comments

@mohamedaymenkarmous
Copy link

mohamedaymenkarmous commented Aug 6, 2019

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: contact@contact.contact
        sender_name: Admin

Thank you for your support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant