1
votes

I have successfully installed FOSUserBundle in my project and everything works as expected. However, I am struggling with how to implement it in my actual project.

I want to create the following setup:

  • A page displaying some user settings in one form (like newsletter subscription), the possibility to change the password in a second form and maybe also a third form to change the username.

  • The settings form as well as some more information is coming from an existing action in my controller and is working well.

I did try a few things but things are not really working out yet:

  • I copied some functionality from FOSUserBundle\Controller\ChangePasswordController\changePasswordAction() to my own action. This way I could get the change password form, create the view and pass it to my template.

  • I added the form to my template with {{ form_widget(form) }}. The form is being displayed and it's even working. I can change the password. However, the labels are being lost, simply reading Current, First, and Second. Also there is no error messaging showing up when the two new passwords don't match or are being left empty.

Over all I have the feeling I am probably doing this in a wrong way. Could you please help me how I should handle this task and point out where I am likely doing something stupid?

Here is the code of my action, reduced to what's important here:

# src/Acme/MyBundle/Controller/BackendController.php
public function accountAction(){

    //pretty much a copy of FOSUserBundle\Controller\ChangePasswordController\changePasswordAction()
    $user = $this->get('security.context')->getToken()->getUser();
    $form = $this->container->get('fos_user.change_password.form');
    $formHandler = $this->container->get('fos_user.change_password.form.handler');

    $process = $formHandler->process($user);
    if ($process) {
       //password has been changed, response will be generated
    }

    //more stuff going on here
    $moreStuff = ...

    //render view
    return $this->render('AcmeMyBundle:Backend:account.html.twig', array(
        'form' => $form->createView(),
        'moreStuff' => $moreStuff
    ));
}
1

1 Answers

0
votes

IMO rendering more than one form in one action is not a good idea. Always try to separate things and let an action handle only one feature.

In your twig template I suggest to use the render method :

{% render 'AcmeBundle:SomeAction' with{'param:param} %}

It will generate a GET request on the action provided with some params if needed.

Create one action that will render the twig template with subrequests :

// AcmeUserBundle:editAction

{% render 'AcmeUserBundle:changePasswordAction' %}
{% render 'AcmeUserBundle:settingsAction' %}
{% render 'AcmeUserBundle:profileAction' %}

And then you'll need to create one action per form.

For password and username modification you can also override FOSUserBundle views if your needs are only visual. If you need to add/remove a field on the form you will need to create a new service.

I sugget reading FOSUserBundle documentation about overriding :

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md#next-steps