I'm building a page which will contain multiple forms. Now when i enter a password mismatch for example i go to the wrong route, namely where the form lives and not the page where i render it.
How do i go at this correctly?
html structure, omitted the 'management' page this is all loaded on
#app/Resources/views/forms/user.html.twig
{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit" class="btn btn-primary pull-right">Submit</button>
{{ form_end(form) }}
#app/Resources/views/components/management/user/user.html.twig
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
{% include 'components/management/user/list.html.twig' %}
</div>
<div class="col-md-6">
{% include 'components/management/user/add.html.twig' %}
</div>
</div>
</div>
#app/Resources/views/components/management/user/add.html.twig
<table class="table">
<thead id="add-user-head" class="pointer">
<tr>
<th>Add user <span id="caret-add-user" class="caret"></span></th>
</tr>
</thead>
<tbody id="add-user-body">
<tr>
<td>
{{ render(controller('AppBundle:Manage:user')) }}
</td>
</tr>
</tbody>
</table>
Controller, omitted the form builder
#@AppBundle/Controller/ManageController.php
public function viewAction()
{
return $this->render('pages/management.html.twig', array('users' => $this->listUsers()));
}
public function userAction(Request $request)
{
// 1) build the form
$user = new User();
$form = $this->createForm(new UserType(), $user, array(
'action' => $this->generateUrl('manage_user'),
'method' => 'GET'
));
// 2) handle the submit (will only happen on POST)
$form->handleRequest($request);
if ($form->isValid() && $form->isSubmitted()) {
// 3) Encode the password (you could also do this via Doctrine listener)
$password = $this->get('security.password_encoder')
->encodePassword($user, $user->getPlainPassword());
$user->setPassword($password);
// 4) save the User!
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
// ... do any other work - like send them an email, etc
// maybe set a "flash" success message for the user
return $this->redirectToRoute('manage');
}
return $this->render('forms/user.html.twig', array('form' => $form->createView()));
}
To clarify a bit more: Invalid input will bring me to the forms/user.html.twig page because of the form action.