You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: user_guide_src/source/libraries/validation.rst
+37-7Lines changed: 37 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
1
1
.. _validation:
2
2
3
+
##########
3
4
Validation
4
5
##########
5
6
@@ -10,6 +11,7 @@ helps minimize the amount of code you'll write.
10
11
:local:
11
12
:depth: 2
12
13
14
+
********
13
15
Overview
14
16
********
15
17
@@ -42,6 +44,7 @@ messages, various control structures are usually placed within the form
42
44
HTML. Form validation, while simple to create, is generally very messy
43
45
and tedious to implement.
44
46
47
+
************************
45
48
Form Validation Tutorial
46
49
************************
47
50
@@ -197,6 +200,7 @@ Then add validation rules in the controller (**Form.php**):
197
200
198
201
If you submit the form you should see the success page or the form with error messages.
199
202
203
+
*********************
200
204
Config for Validation
201
205
*********************
202
206
@@ -238,6 +242,7 @@ If you want to use traditional rules, you need to change the rule classes in **a
238
242
239
243
.. literalinclude:: validation/003.php
240
244
245
+
*******************
241
246
Loading the Library
242
247
*******************
243
248
@@ -251,6 +256,7 @@ for including multiple Rulesets, and collections of rules that can be easily reu
251
256
.. note:: You may never need to use this method, as both the :doc:`Controller </incoming/controllers>` and
252
257
the :doc:`Model </models/model>` provide methods to make validation even easier.
253
258
259
+
************************
254
260
Setting Validation Rules
255
261
************************
256
262
@@ -311,6 +317,7 @@ data to be validated:
311
317
is not HTML form post (``Content-Type: multipart/form-data``),
312
318
or gets data from :ref:`$request->getVar() <incomingrequest-getting-data>`.
313
319
320
+
***********************
314
321
Working with Validation
315
322
***********************
316
323
@@ -426,13 +433,14 @@ So it will ignore the row in the database that has ``id=4`` when it verifies the
426
433
This can also be used to create more dynamic rules at runtime, as long as you take care that any dynamic
427
434
keys passed in don't conflict with your form data.
428
435
429
-
Working With Errors
436
+
*******************
437
+
Working with Errors
430
438
*******************
431
439
432
440
The Validation library provides several methods to help you set error messages, provide
433
441
custom error messages, and retrieve one or more errors to display.
434
442
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
436
444
each rule has an entry.
437
445
438
446
.. _validation-custom-errors:
@@ -467,11 +475,11 @@ at least 6 characters."
467
475
468
476
.. 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.
469
477
470
-
Translation Of Messages And Validation Labels
478
+
Translation of Messages and Validation Labels
471
479
=============================================
472
480
473
481
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**.
475
483
We can simply use the language lines defined in this file, like this:
476
484
477
485
.. literalinclude:: validation/025.php
@@ -530,8 +538,28 @@ When specifying a field with a wildcard, all errors matching the mask will be ch
530
538
531
539
.. literalinclude:: validation/029.php
532
540
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
+
533
560
.. _validation-customizing-error-display:
534
561
562
+
*************************
535
563
Customizing Error Display
536
564
*************************
537
565
@@ -562,7 +590,7 @@ error message. This is used with the ``showError()`` method where a field must b
562
590
Configuration
563
591
=============
564
592
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**.
566
594
Inside, you'll find the ``$templates`` property where you can list as many custom views as you want, and provide an
567
595
short alias they can be referenced by. If we were to add our example file from above, it would look something like:
568
596
@@ -580,6 +608,7 @@ right after the name of the field the error should belong to::
0 commit comments