Reputation: 938
I'd like to integrate the change_password form of FOSUserBundle in my settings page where I have also change_email form or other information. I know how to integrate a FOS for in my design but not how to do what I'm trying to do.
Now, the form is generate through the controller and a form builder method and I don't know how to modify this...
Thanks in advance, Valentin
Upvotes: 2
Views: 6097
Reputation: 116
(I see that this is an 8 months old question, but maybe it helps someone.)
The most simple way is to use a separate form for the password change event if you want to make only one page for the user profile. FOSUserBundle provides this functionality. So if you want to use your own routes and forms, you can just copy the code from the FOS controller and forms, change some parameters (like route names and design), and you are set. There are probably some more sophisticated ways to use this Bundle, but in my opinion this is the easiest and most flexible.
The FOSUserBundle is in the /vendor/friendsofsymfony/user-bundle/FOS/UserBundle/
directory.
The password controller is at /Controller/ChangePasswordController.php
and the forms are in /Resources/views/ChangePassword
.
Here is how I did this in my Settings page. I only use the password change functionality, but I assume, you can have separate actions for the different forms, and then redirect the user back to the original index page with the view.
This is the controller (I only changed the redirect route and the view):
use JMS\SecurityExtraBundle\Annotation\Secure;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use FOS\UserBundle\Model\UserInterface;
class SettingsController extends Controller
{
/**
* @Secure(roles="ROLE_USER")
*/
public function indexAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
$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) {
$this->get('session')->setFlash('notice', 'Password changed succesfully');
return $this->redirect($this->generateUrl('settings'));
}
return $this->render('AcmeHelloBundle:Settings:password.html.twig', ['form' => $form->createView()]);
}
}
And this is the view (password.html.twig) - the only change here is the route: path('settings')
<form action="{{ path('settings') }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password">
{{ form_widget(form) }}
<div>
<input type="submit" value="{{ 'change_password.submit'|trans({}, 'FOSUserBundle') }}" />
</div>
</form>
So this is it. Now you have a nice password change form, and all the heavy lifting is taken care by the FOS UserBundle!
Upvotes: 7