I'm overriding the forms on my Symfony app, but I surely skipped something in the process, but I do not know what.
Basically, it all works fine and looking as I want it to, but as soon as I purposely generate an error(i.e : changing email address to an invalid one), I am redirected to the lonely form template, instead of reloading to my page with the generated and displaying the problem.
I tried replacing this line :
return $this->render('@FOSUser/Profile/edit.html.twig', array(
'form' => $form->createView(),
));
from the ProfileController, as I think it is the reason, but I'm doing it wrong and getting errors when I try.
What would be the correct syntax to go my customized profile page that contains other forms, while displaying the errors of the submitted form ?
I assume you have already overridden FOSUserBundle by your own UserBundle (as explained in the official documentation). Then, you have to modify the function editAction()
in your own ProfileController
, and write the twig template of your UserBundle profile page (look at my last comment in the code below):
<?php
// src/UserBundle/Controller/ProfileController.php
namespace UserBundle\Controller;
// use statements
class ProfileController extends Controller
{
/**
* Edit the user.
*
* @param Request $request
*
* @return Response
*/
public function editAction(Request $request)
{
$user = $this->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
/** @var $dispatcher EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
/** @var $formFactory FactoryInterface */
$formFactory = $this->get('fos_user.profile.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var $userManager UserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_profile_show');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
// Change the following line, with your custom profile twig template
//return $this->render('@FOSUser/Profile/edit.html.twig', array(
return $this->render('UserBundle:Profile:edit.html.twig', array(
'form' => $form->createView(),
));
}
}