Skip to content

Commit 93cf9bd

Browse files
Add the _failure_path hidden field in template
1 parent 814d8c4 commit 93cf9bd

File tree

1 file changed

+72
-65
lines changed

1 file changed

+72
-65
lines changed

security/form_login.rst

Lines changed: 72 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -218,57 +218,14 @@ this by setting ``use_referer`` to true (it defaults to false):
218218
),
219219
));
220220
221-
Control the Redirect URL from inside the Form
222-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
223-
224-
You can also override where the user is redirected to via the form itself by
225-
including a hidden field with the name ``_target_path``. For example, to
226-
redirect to the URL defined by some ``account`` route, use the following:
227-
228-
.. configuration-block::
229-
230-
.. code-block:: html+twig
231-
232-
{# src/AppBundle/Resources/views/Security/login.html.twig #}
233-
{% if error %}
234-
<div>{{ error.message }}</div>
235-
{% endif %}
236-
237-
<form action="{{ path('login') }}" method="post">
238-
<label for="username">Username:</label>
239-
<input type="text" id="username" name="_username" value="{{ last_username }}" />
240-
241-
<label for="password">Password:</label>
242-
<input type="password" id="password" name="_password" />
243-
244-
<input type="hidden" name="_target_path" value="account" />
245-
246-
<input type="submit" name="login" />
247-
</form>
248-
249-
.. code-block:: html+php
250-
251-
<!-- src/AppBundle/Resources/views/Security/login.html.php -->
252-
<?php if ($error): ?>
253-
<div><?php echo $error->getMessage() ?></div>
254-
<?php endif ?>
255-
256-
<form action="<?php echo $view['router']->generate('login') ?>" method="post">
257-
<label for="username">Username:</label>
258-
<input type="text" id="username" name="_username" value="<?php echo $last_username ?>" />
259-
260-
<label for="password">Password:</label>
261-
<input type="password" id="password" name="_password" />
262-
263-
<input type="hidden" name="_target_path" value="account" />
264-
265-
<input type="submit" name="login" />
266-
</form>
221+
Redirecting on Login Failure
222+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
267223

268-
Now, the user will be redirected to the value of the hidden form field. The
269-
value attribute can be a relative path, absolute URL, or a route name. You
270-
can even change the name of the hidden form field by changing the ``target_path_parameter``
271-
option to another value.
224+
In addition to redirecting the user after a successful login, you can also set
225+
the URL that the user should be redirected to after a failed login (e.g. an
226+
invalid username or password was submitted). By default, the user is redirected
227+
back to the login form itself. You can set this to a different route (e.g.
228+
``login_failure``) with the following config:
272229

273230
.. configuration-block::
274231

@@ -282,7 +239,8 @@ option to another value.
282239
main:
283240
# ...
284241
form_login:
285-
target_path_parameter: redirect_url
242+
# ...
243+
failure_path: login_failure
286244
287245
.. code-block:: xml
288246
@@ -299,7 +257,7 @@ option to another value.
299257
300258
<firewall name="main">
301259
<!-- ... -->
302-
<form-login target-path-parameter="redirect_url" />
260+
<form-login failure-path="login_failure" />
303261
</firewall>
304262
</config>
305263
</srv:container>
@@ -314,20 +272,68 @@ option to another value.
314272
'main' => array(
315273
// ...
316274
'form_login' => array(
317-
'target_path_parameter' => 'redirect_url',
275+
// ...
276+
'failure_path' => 'login_failure',
318277
),
319278
),
320279
),
321280
));
322281
323-
Redirecting on Login Failure
324-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
282+
Control the Redirect URL from inside the Form
283+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
325284

326-
In addition to redirecting the user after a successful login, you can also set
327-
the URL that the user should be redirected to after a failed login (e.g. an
328-
invalid username or password was submitted). By default, the user is redirected
329-
back to the login form itself. You can set this to a different route (e.g.
330-
``login_failure``) with the following config:
285+
You can also override where the user is redirected to via the form itself by
286+
including a hidden field with the name ``_target_path`` for success and
287+
``_failure_path`` for failure. For example, to redirect to the URL defined
288+
by some ``account`` route, use the following:
289+
290+
.. configuration-block::
291+
292+
.. code-block:: html+twig
293+
294+
{# src/AppBundle/Resources/views/Security/login.html.twig #}
295+
{% if error %}
296+
<div>{{ error.message }}</div>
297+
{% endif %}
298+
299+
<form action="{{ path('login') }}" method="post">
300+
<label for="username">Username:</label>
301+
<input type="text" id="username" name="_username" value="{{ last_username }}" />
302+
303+
<label for="password">Password:</label>
304+
<input type="password" id="password" name="_password" />
305+
306+
<input type="hidden" name="_target_path" value="account" />
307+
<input type="hidden" name="_failure_path" value="login" />
308+
309+
<input type="submit" name="login" />
310+
</form>
311+
312+
.. code-block:: html+php
313+
314+
<!-- src/AppBundle/Resources/views/Security/login.html.php -->
315+
<?php if ($error): ?>
316+
<div><?php echo $error->getMessage() ?></div>
317+
<?php endif ?>
318+
319+
<form action="<?php echo $view['router']->path('login') ?>" method="post">
320+
<label for="username">Username:</label>
321+
<input type="text" id="username" name="_username" value="<?php echo $last_username ?>" />
322+
323+
<label for="password">Password:</label>
324+
<input type="password" id="password" name="_password" />
325+
326+
<input type="hidden" name="_target_path" value="account" />
327+
<input type="hidden" name="_failure_path" value="login" />
328+
329+
<input type="submit" name="login" />
330+
</form>
331+
332+
Now, the user will be redirected to the value of the hidden form field. The
333+
value attribute can be a relative path, absolute URL, or a route name.
334+
You can even change the name of the hidden form field by changing the
335+
``target_path_parameter`` and ``failure_path_parameter`` options to another
336+
value.
331337

332338
.. configuration-block::
333339

@@ -341,8 +347,8 @@ back to the login form itself. You can set this to a different route (e.g.
341347
main:
342348
# ...
343349
form_login:
344-
# ...
345-
failure_path: login_failure
350+
target_path_parameter: redirect_url
351+
failure_path_parameter: redirect_url
346352
347353
.. code-block:: xml
348354
@@ -359,7 +365,8 @@ back to the login form itself. You can set this to a different route (e.g.
359365
360366
<firewall name="main">
361367
<!-- ... -->
362-
<form-login failure-path="login_failure" />
368+
<form-login target-path-parameter="redirect_url" />
369+
<form-login failure-path-parameter="redirect_url" />
363370
</firewall>
364371
</config>
365372
</srv:container>
@@ -374,8 +381,8 @@ back to the login form itself. You can set this to a different route (e.g.
374381
'main' => array(
375382
// ...
376383
'form_login' => array(
377-
// ...
378-
'failure_path' => 'login_failure',
384+
'target_path_parameter' => 'redirect_url',
385+
'failure_path_parameter' => 'redirect_url',
379386
),
380387
),
381388
),

0 commit comments

Comments
 (0)