Skip to content

Commit d483d57

Browse files
authored
Merge pull request #7343 from kenjis/fix-docs-validation
docs: update Validation
2 parents 536dab6 + 6f1db3c commit d483d57

3 files changed

Lines changed: 44 additions & 8 deletions

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: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.. _validation:
22

3+
##########
34
Validation
45
##########
56

@@ -10,6 +11,7 @@ helps minimize the amount of code you'll write.
1011
:local:
1112
:depth: 2
1213

14+
********
1315
Overview
1416
********
1517

@@ -42,6 +44,7 @@ messages, various control structures are usually placed within the form
4244
HTML. Form validation, while simple to create, is generally very messy
4345
and tedious to implement.
4446

47+
************************
4548
Form Validation Tutorial
4649
************************
4750

@@ -197,6 +200,7 @@ Then add validation rules in the controller (**Form.php**):
197200

198201
If you submit the form you should see the success page or the form with error messages.
199202

203+
*********************
200204
Config for Validation
201205
*********************
202206

@@ -238,6 +242,7 @@ If you want to use traditional rules, you need to change the rule classes in **a
238242

239243
.. literalinclude:: validation/003.php
240244

245+
*******************
241246
Loading the Library
242247
*******************
243248

@@ -251,6 +256,7 @@ for including multiple Rulesets, and collections of rules that can be easily reu
251256
.. note:: You may never need to use this method, as both the :doc:`Controller </incoming/controllers>` and
252257
the :doc:`Model </models/model>` provide methods to make validation even easier.
253258

259+
************************
254260
Setting Validation Rules
255261
************************
256262

@@ -311,6 +317,7 @@ data to be validated:
311317
is not HTML form post (``Content-Type: multipart/form-data``),
312318
or gets data from :ref:`$request->getVar() <incomingrequest-getting-data>`.
313319

320+
***********************
314321
Working with Validation
315322
***********************
316323

@@ -426,13 +433,14 @@ So it will ignore the row in the database that has ``id=4`` when it verifies the
426433
This can also be used to create more dynamic rules at runtime, as long as you take care that any dynamic
427434
keys passed in don't conflict with your form data.
428435

429-
Working With Errors
436+
*******************
437+
Working with Errors
430438
*******************
431439

432440
The Validation library provides several methods to help you set error messages, provide
433441
custom error messages, and retrieve one or more errors to display.
434442

435-
By default, error messages are derived from language strings in ``system/Language/en/Validation.php``, where
443+
By default, error messages are derived from language strings in **system/Language/en/Validation.php**, where
436444
each rule has an entry.
437445

438446
.. _validation-custom-errors:
@@ -467,11 +475,11 @@ at least 6 characters."
467475

468476
.. note:: When using label-style error messages, if you pass the second parameter to ``setRules()``, it will be overwritten with the value of the first parameter.
469477

470-
Translation Of Messages And Validation Labels
478+
Translation of Messages and Validation Labels
471479
=============================================
472480

473481
To use translated strings from language files, we can simply use the dot syntax.
474-
Let's say we have a file with translations located here: ``app/Languages/en/Rules.php``.
482+
Let's say we have a file with translations located here: **app/Languages/en/Rules.php**.
475483
We can simply use the language lines defined in this file, like this:
476484

477485
.. literalinclude:: validation/025.php
@@ -530,8 +538,28 @@ When specifying a field with a wildcard, all errors matching the mask will be ch
530538

531539
.. literalinclude:: validation/029.php
532540

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+
533560
.. _validation-customizing-error-display:
534561

562+
*************************
535563
Customizing Error Display
536564
*************************
537565

@@ -562,7 +590,7 @@ error message. This is used with the ``showError()`` method where a field must b
562590
Configuration
563591
=============
564592

565-
Once you have your views created, you need to let the Validation library know about them. Open ``Config/Validation.php``.
593+
Once you have your views created, you need to let the Validation library know about them. Open **app/Config/Validation.php**.
566594
Inside, you'll find the ``$templates`` property where you can list as many custom views as you want, and provide an
567595
short alias they can be referenced by. If we were to add our example file from above, it would look something like:
568596

@@ -580,6 +608,7 @@ right after the name of the field the error should belong to::
580608

581609
<?= $validation->showError('username', 'my_single') ?>
582610

611+
*********************
583612
Creating Custom Rules
584613
*********************
585614

@@ -592,7 +621,7 @@ autoloader can find it. These files are called RuleSets.
592621
Adding a RuleSet
593622
----------------
594623

595-
To add a new RuleSet, edit **Config/Validation.php** and
624+
To add a new RuleSet, edit **app/Config/Validation.php** and
596625
add the new file to the ``$ruleSets`` array:
597626

598627
.. literalinclude:: validation/033.php
@@ -608,7 +637,7 @@ a boolean true or false value signifying true if it passed the test or false if
608637

609638
.. literalinclude:: validation/034.php
610639

611-
By default, the system will look within ``CodeIgniter\Language\en\Validation.php`` for the language strings used
640+
By default, the system will look within **system/Language/en/Validation.php** for the language strings used
612641
within errors. In custom rules, you may provide error messages by accepting a ``&$error`` variable by reference in the
613642
second parameter:
614643

@@ -654,6 +683,7 @@ Or you can use the following parameters:
654683

655684
.. literalinclude:: validation/041.php
656685

686+
***************
657687
Available Rules
658688
***************
659689

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)