Skip to content

Commit 6f1db3c

Browse files
committed
docs: add "Redirect and Validation Errors"
1 parent a2f2dea commit 6f1db3c

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

user_guide_src/source/helpers/form_helper.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ The following functions are available:
534534
that are stored in the session. To store the errors in the session, you need to use ``withInput()`` with :php:func:`redirect() <redirect>`.
535535

536536
The returned array is the same as ``Validation::getErrors()``.
537-
See :ref:`Validation <validation-getting-all-errors>` for details.
537+
See :ref:`Validation <validation-redirect-and-validation-errors>` for details.
538538

539539
Example::
540540

user_guide_src/source/libraries/validation.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,25 @@ When specifying a field with a wildcard, all errors matching the mask will be ch
538538

539539
.. literalinclude:: validation/029.php
540540

541+
.. _validation-redirect-and-validation-errors:
542+
543+
Redirect and Validation Errors
544+
==============================
545+
546+
PHP shares nothing between requests. So when you redirect if a validation fails,
547+
there will be no validation errors in the redirected request because the validation
548+
has run in the previous request.
549+
550+
In that case, you need to use Form helper function :php:func:`validation_errors()`,
551+
:php:func:`validation_list_errors()` and :php:func:`validation_show_error()`.
552+
These functions check the validation errors that are stored in the session.
553+
554+
To store the validation errors in the session, you need to use ``withInput()``
555+
with :php:func:`redirect() <redirect>`:
556+
557+
.. literalinclude:: validation/042.php
558+
:lines: 2-
559+
541560
.. _validation-customizing-error-display:
542561

543562
*************************
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// In Controller.
4+
if (! $this->validateData($data, $rules)) {
5+
return redirect()->back()->withInput();
6+
}

0 commit comments

Comments
 (0)