Skip to content

Commit 4de8599

Browse files
committed
minor #7620 Add the _failure_path hidden field in template (damienalexandre, javiereguiluz)
This PR was merged into the 2.7 branch. Discussion ---------- Add the `_failure_path` hidden field in template Today I learnt about this [feature](symfony/symfony@d0057d0) so I guess it was time to document it. Basically, what we can do with the `_target_path` form field is also possible for failure via the `_failure_path` field. It's very useful when you want multiple login forms for the same firewall (a general login, and a login during e-commerce checkout for example). I also moved the sections to me more logic, as the last section was about **Redirecting on Login Failure** via the option - because it was not explained yet in the page. This section is now just before the updated **Control the Redirect URL from inside the Form**. Commits ------- a865da1 Use different values for target_path_parameter and failure_path_parameter 1fc80af Minor rewords 93cf9bd Add the `_failure_path` hidden field in template
2 parents 6ecfd53 + a865da1 commit 4de8599

File tree

1 file changed

+68
-65
lines changed

1 file changed

+68
-65
lines changed

security/form_login.rst

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -218,57 +218,12 @@ 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+
After a failed login (e.g. an invalid username or password was submitted), the
225+
user is redirected back to the login form itself. Use the ``failure_path``
226+
option to define the route or URL the user is redirected to:
272227

273228
.. configuration-block::
274229

@@ -282,7 +237,8 @@ option to another value.
282237
main:
283238
# ...
284239
form_login:
285-
target_path_parameter: redirect_url
240+
# ...
241+
failure_path: login_failure
286242
287243
.. code-block:: xml
288244
@@ -299,7 +255,7 @@ option to another value.
299255
300256
<firewall name="main">
301257
<!-- ... -->
302-
<form-login target-path-parameter="redirect_url" />
258+
<form-login failure-path="login_failure" />
303259
</firewall>
304260
</config>
305261
</srv:container>
@@ -314,20 +270,66 @@ option to another value.
314270
'main' => array(
315271
// ...
316272
'form_login' => array(
317-
'target_path_parameter' => 'redirect_url',
273+
// ...
274+
'failure_path' => 'login_failure',
318275
),
319276
),
320277
),
321278
));
322279
323-
Redirecting on Login Failure
324-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
280+
Control the Redirect URL from inside the Form
281+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
325282

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:
283+
You can also override where the user is redirected to via the form itself by
284+
including a hidden field with the name ``_target_path`` for successful logins
285+
and ``_failure_path`` for login errors:
286+
287+
.. configuration-block::
288+
289+
.. code-block:: html+twig
290+
291+
{# src/AppBundle/Resources/views/Security/login.html.twig #}
292+
{% if error %}
293+
<div>{{ error.message }}</div>
294+
{% endif %}
295+
296+
<form action="{{ path('login') }}" method="post">
297+
<label for="username">Username:</label>
298+
<input type="text" id="username" name="_username" value="{{ last_username }}" />
299+
300+
<label for="password">Password:</label>
301+
<input type="password" id="password" name="_password" />
302+
303+
<input type="hidden" name="_target_path" value="account" />
304+
<input type="hidden" name="_failure_path" value="login" />
305+
306+
<input type="submit" name="login" />
307+
</form>
308+
309+
.. code-block:: html+php
310+
311+
<!-- src/AppBundle/Resources/views/Security/login.html.php -->
312+
<?php if ($error): ?>
313+
<div><?php echo $error->getMessage() ?></div>
314+
<?php endif ?>
315+
316+
<form action="<?php echo $view['router']->path('login') ?>" method="post">
317+
<label for="username">Username:</label>
318+
<input type="text" id="username" name="_username" value="<?php echo $last_username ?>" />
319+
320+
<label for="password">Password:</label>
321+
<input type="password" id="password" name="_password" />
322+
323+
<input type="hidden" name="_target_path" value="account" />
324+
<input type="hidden" name="_failure_path" value="login" />
325+
326+
<input type="submit" name="login" />
327+
</form>
328+
329+
Now, the user will be redirected to the value of the hidden form field. The
330+
value attribute can be a relative path, absolute URL, or a route name.
331+
The name of the hidden fields in the login form is also configurable using the
332+
``target_path_parameter`` and ``failure_path_parameter`` options of the firewall.
331333

332334
.. configuration-block::
333335

@@ -341,8 +343,8 @@ back to the login form itself. You can set this to a different route (e.g.
341343
main:
342344
# ...
343345
form_login:
344-
# ...
345-
failure_path: login_failure
346+
target_path_parameter: login_success
347+
failure_path_parameter: login_fail
346348
347349
.. code-block:: xml
348350
@@ -359,7 +361,8 @@ back to the login form itself. You can set this to a different route (e.g.
359361
360362
<firewall name="main">
361363
<!-- ... -->
362-
<form-login failure-path="login_failure" />
364+
<form-login target-path-parameter="login_success" />
365+
<form-login failure-path-parameter="login_fail" />
363366
</firewall>
364367
</config>
365368
</srv:container>
@@ -374,8 +377,8 @@ back to the login form itself. You can set this to a different route (e.g.
374377
'main' => array(
375378
// ...
376379
'form_login' => array(
377-
// ...
378-
'failure_path' => 'login_failure',
380+
'target_path_parameter' => 'login_success',
381+
'failure_path_parameter' => 'login_fail',
379382
),
380383
),
381384
),

0 commit comments

Comments
 (0)