Skip to content

Add the _failure_path hidden field in template #7620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 68 additions & 65 deletions security/form_login.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,57 +218,12 @@ this by setting ``use_referer`` to true (it defaults to false):
),
));

Control the Redirect URL from inside the Form
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also override where the user is redirected to via the form itself by
including a hidden field with the name ``_target_path``. For example, to
redirect to the URL defined by some ``account`` route, use the following:

.. configuration-block::

.. code-block:: html+twig

{# src/AppBundle/Resources/views/Security/login.html.twig #}
{% if error %}
<div>{{ error.message }}</div>
{% endif %}

<form action="{{ path('login') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />

<label for="password">Password:</label>
<input type="password" id="password" name="_password" />

<input type="hidden" name="_target_path" value="account" />

<input type="submit" name="login" />
</form>

.. code-block:: html+php

<!-- src/AppBundle/Resources/views/Security/login.html.php -->
<?php if ($error): ?>
<div><?php echo $error->getMessage() ?></div>
<?php endif ?>

<form action="<?php echo $view['router']->generate('login') ?>" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="<?php echo $last_username ?>" />

<label for="password">Password:</label>
<input type="password" id="password" name="_password" />

<input type="hidden" name="_target_path" value="account" />

<input type="submit" name="login" />
</form>
Redirecting on Login Failure
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now, the user will be redirected to the value of the hidden form field. The
value attribute can be a relative path, absolute URL, or a route name. You
can even change the name of the hidden form field by changing the ``target_path_parameter``
option to another value.
After a failed login (e.g. an invalid username or password was submitted), the
user is redirected back to the login form itself. Use the ``failure_path``
option to define the route or URL the user is redirected to:

.. configuration-block::

Expand All @@ -282,7 +237,8 @@ option to another value.
main:
# ...
form_login:
target_path_parameter: redirect_url
# ...
failure_path: login_failure

.. code-block:: xml

Expand All @@ -299,7 +255,7 @@ option to another value.

<firewall name="main">
<!-- ... -->
<form-login target-path-parameter="redirect_url" />
<form-login failure-path="login_failure" />
</firewall>
</config>
</srv:container>
Expand All @@ -314,20 +270,66 @@ option to another value.
'main' => array(
// ...
'form_login' => array(
'target_path_parameter' => 'redirect_url',
// ...
'failure_path' => 'login_failure',
),
),
),
));

Redirecting on Login Failure
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Control the Redirect URL from inside the Form
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In addition to redirecting the user after a successful login, you can also set
the URL that the user should be redirected to after a failed login (e.g. an
invalid username or password was submitted). By default, the user is redirected
back to the login form itself. You can set this to a different route (e.g.
``login_failure``) with the following config:
You can also override where the user is redirected to via the form itself by
including a hidden field with the name ``_target_path`` for successful logins
and ``_failure_path`` for login errors:

.. configuration-block::

.. code-block:: html+twig

{# src/AppBundle/Resources/views/Security/login.html.twig #}
{% if error %}
<div>{{ error.message }}</div>
{% endif %}

<form action="{{ path('login') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />

<label for="password">Password:</label>
<input type="password" id="password" name="_password" />

<input type="hidden" name="_target_path" value="account" />
<input type="hidden" name="_failure_path" value="login" />

<input type="submit" name="login" />
</form>

.. code-block:: html+php

<!-- src/AppBundle/Resources/views/Security/login.html.php -->
<?php if ($error): ?>
<div><?php echo $error->getMessage() ?></div>
<?php endif ?>

<form action="<?php echo $view['router']->path('login') ?>" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="<?php echo $last_username ?>" />

<label for="password">Password:</label>
<input type="password" id="password" name="_password" />

<input type="hidden" name="_target_path" value="account" />
<input type="hidden" name="_failure_path" value="login" />

<input type="submit" name="login" />
</form>

Now, the user will be redirected to the value of the hidden form field. The
value attribute can be a relative path, absolute URL, or a route name.
The name of the hidden fields in the login form is also configurable using the
``target_path_parameter`` and ``failure_path_parameter`` options of the firewall.

.. configuration-block::

Expand All @@ -341,8 +343,8 @@ back to the login form itself. You can set this to a different route (e.g.
main:
# ...
form_login:
# ...
failure_path: login_failure
target_path_parameter: login_success
failure_path_parameter: login_fail

.. code-block:: xml

Expand All @@ -359,7 +361,8 @@ back to the login form itself. You can set this to a different route (e.g.

<firewall name="main">
<!-- ... -->
<form-login failure-path="login_failure" />
<form-login target-path-parameter="login_success" />
<form-login failure-path-parameter="login_fail" />
</firewall>
</config>
</srv:container>
Expand All @@ -374,8 +377,8 @@ back to the login form itself. You can set this to a different route (e.g.
'main' => array(
// ...
'form_login' => array(
// ...
'failure_path' => 'login_failure',
'target_path_parameter' => 'login_success',
'failure_path_parameter' => 'login_fail',
),
),
),
Expand Down