Skip to content

Commit e5407b7

Browse files
authored
Merge pull request #7423 from kenjis/fix-docs-validation.rst
docs: fix validation.rst
2 parents 987f3da + 4444041 commit e5407b7

7 files changed

Lines changed: 109 additions & 79 deletions

File tree

user_guide_src/source/incoming/controllers.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ inside the controller:
6666

6767
.. literalinclude:: controllers/001.php
6868

69-
.. _controllers-validating-data:
70-
7169
forceHTTPS
7270
**********
7371

@@ -84,11 +82,13 @@ modify this by passing the duration (in seconds) as the first parameter:
8482

8583
.. note:: A number of :doc:`time-based constants </general/common_functions>` are always available for you to use, including ``YEAR``, ``MONTH``, and more.
8684

87-
.. _controller-validate:
85+
.. _controllers-validating-data:
8886

8987
Validating Data
9088
***************
9189

90+
.. _controller-validate:
91+
9292
$this->validate()
9393
=================
9494

user_guide_src/source/libraries/validation.rst

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,11 @@ given field, cascading them in order. To set validation rules you
265265
will use the ``setRule()``, ``setRules()``, or ``withRequest()``
266266
methods.
267267

268+
Setting a Single Rule
269+
=====================
270+
268271
setRule()
269-
=========
272+
---------
270273

271274
This method sets a single rule. It has the method signature::
272275

@@ -285,8 +288,11 @@ the form input name.
285288
broken in extending classes overriding this method, the child class's method should also be modified
286289
to remove the typehint.
287290

291+
Setting Multiple Rules
292+
======================
293+
288294
setRules()
289-
==========
295+
----------
290296

291297
Like ``setRule()``, but accepts an array of field names and their rules:
292298

@@ -298,6 +304,23 @@ To give a labeled error message you can set up as:
298304

299305
.. _validation-withrequest:
300306

307+
Setting Rules for Array Data
308+
============================
309+
310+
If your data is in a nested associative array, you can use "dot array syntax" to
311+
easily validate your data:
312+
313+
.. literalinclude:: validation/009.php
314+
315+
You can use the ``*`` wildcard symbol to match any one level of the array:
316+
317+
.. literalinclude:: validation/010.php
318+
319+
"dot array syntax" can also be useful when you have single dimension array data.
320+
For example, data returned by multi select dropdown:
321+
322+
.. literalinclude:: validation/011.php
323+
301324
withRequest()
302325
=============
303326

@@ -321,25 +344,37 @@ data to be validated:
321344
Working with Validation
322345
***********************
323346

324-
Validating Keys that are Arrays
325-
===============================
347+
Running Validation
348+
==================
326349

327-
If your data is in a nested associative array, you can use "dot array syntax" to
328-
easily validate your data:
350+
The ``run()`` method runs validation. It has the method signature::
329351

330-
.. literalinclude:: validation/009.php
352+
run(?array $data = null, ?string $group = null, ?string $dbGroup = null): bool
331353

332-
You can use the '*' wildcard symbol to match any one level of the array:
354+
The ``$data`` is an array of data to validate. The optional second parameter
355+
``$group`` is the :ref:`predefined group of rules <validation-array>` to apply.
356+
The optional third parameter ``$dbGroup`` is the database group to use.
333357

334-
.. literalinclude:: validation/010.php
358+
This method returns true if the validation is successful.
335359

336-
"dot array syntax" can also be useful when you have single dimension array data.
337-
For example, data returned by multi select dropdown:
360+
.. literalinclude:: validation/043.php
338361

339-
.. literalinclude:: validation/011.php
362+
Running Multiple Validations
363+
============================
340364

341-
Validate 1 Value
342-
================
365+
.. note:: ``run()`` method will not reset error state. Should a previous run fail,
366+
``run()`` will always return false and ``getErrors()`` will return
367+
all previous errors until explicitly reset.
368+
369+
If you intend to run multiple validations, for instance on different data sets or with different
370+
rules after one another, you might need to call ``$validation->reset()`` before each run to get rid of
371+
errors from previous run. Be aware that ``reset()`` will invalidate any data, rule or custom error
372+
you previously set, so ``setRules()``, ``setRuleGroup()`` etc. need to be repeated:
373+
374+
.. literalinclude:: validation/019.php
375+
376+
Validating 1 Value
377+
==================
343378

344379
Validate one value against a rule:
345380

@@ -355,7 +390,7 @@ the validation.
355390

356391
.. _validation-array:
357392

358-
How to save your rules
393+
How to Save Your Rules
359394
----------------------
360395

361396
To store your validation rules, simply create a new public property in the ``Config\Validation``
@@ -364,10 +399,16 @@ rules. As shown earlier, the validation array will have this prototype:
364399

365400
.. literalinclude:: validation/013.php
366401

402+
How to Specify Rule Group
403+
-------------------------
404+
367405
You can specify the group to use when you call the ``run()`` method:
368406

369407
.. literalinclude:: validation/014.php
370408

409+
How to Save Error Messages
410+
--------------------------
411+
371412
You can also store custom error messages in this configuration file by naming the
372413
property the same as the group, and appended with ``_errors``. These will automatically
373414
be used for any errors when this group is used:
@@ -378,7 +419,7 @@ Or pass all settings in an array:
378419

379420
.. literalinclude:: validation/016.php
380421

381-
See below for details on the formatting of the array.
422+
See :ref:`validation-custom-errors` for details on the formatting of the array.
382423

383424
Getting & Setting Rule Groups
384425
-----------------------------
@@ -395,20 +436,6 @@ This method sets a rule group from the validation configuration to the validatio
395436

396437
.. literalinclude:: validation/018.php
397438

398-
Running Multiple Validations
399-
============================
400-
401-
.. note:: ``run()`` method will not reset error state. Should a previous run fail,
402-
``run()`` will always return false and ``getErrors()`` will return
403-
all previous errors until explicitly reset.
404-
405-
If you intend to run multiple validations, for instance on different data sets or with different
406-
rules after one another, you might need to call ``$validation->reset()`` before each run to get rid of
407-
errors from previous run. Be aware that ``reset()`` will invalidate any data, rule or custom error
408-
you previously set, so ``setRules()``, ``setRuleGroup()`` etc. need to be repeated:
409-
410-
.. literalinclude:: validation/019.php
411-
412439
Validation Placeholders
413440
=======================
414441

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
<?php
22

3-
$validation->setRules(
4-
[
5-
'username' => [
6-
'label' => 'Username',
7-
'rules' => 'required|is_unique[users.username]',
8-
'errors' => [
9-
'required' => 'All accounts must have {field} provided',
10-
],
3+
$validation->setRules([
4+
'username' => [
5+
'label' => 'Username',
6+
'rules' => 'required|is_unique[users.username]',
7+
'errors' => [
8+
'required' => 'All accounts must have {field} provided',
119
],
12-
'password' => [
13-
'label' => 'Password',
14-
'rules' => 'required|min_length[10]',
15-
'errors' => [
16-
'min_length' => 'Your {field} is too short. You want to get hacked?',
17-
],
10+
],
11+
'password' => [
12+
'label' => 'Password',
13+
'rules' => 'required|min_length[10]',
14+
'errors' => [
15+
'min_length' => 'Your {field} is too short. You want to get hacked?',
1816
],
19-
]
20-
);
17+
],
18+
]);
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
<?php
22

3-
$validation->setRules(
4-
[
5-
'username' => [
6-
'label' => 'Rules.username',
7-
'rules' => 'required|is_unique[users.username]',
8-
'errors' => [
9-
'required' => 'Rules.username.required',
10-
],
3+
$validation->setRules([
4+
'username' => [
5+
'label' => 'Rules.username',
6+
'rules' => 'required|is_unique[users.username]',
7+
'errors' => [
8+
'required' => 'Rules.username.required',
119
],
12-
'password' => [
13-
'label' => 'Rules.password',
14-
'rules' => 'required|min_length[10]',
15-
'errors' => [
16-
'min_length' => 'Rules.password.min_length',
17-
],
10+
],
11+
'password' => [
12+
'label' => 'Rules.password',
13+
'rules' => 'required|min_length[10]',
14+
'errors' => [
15+
'min_length' => 'Rules.password.min_length',
1816
],
19-
]
20-
);
17+
],
18+
]);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

3-
$this->validate($request, [
3+
$validation->setRules([
44
'foo' => 'required|even',
55
]);
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
<?php
22

3-
$validation->setRules(
4-
[
5-
'foo' => [
6-
'required',
7-
static function ($value, $data, &$error, $field) {
8-
if ((int) $value % 2 === 0) {
9-
return true;
10-
}
3+
$validation->setRules([
4+
'foo' => [
5+
'required',
6+
static function ($value, $data, &$error, $field) {
7+
if ((int) $value % 2 === 0) {
8+
return true;
9+
}
1110

12-
$error = 'The value is not even.';
11+
$error = 'The value is not even.';
1312

14-
return false;
15-
},
16-
],
13+
return false;
14+
},
1715
],
18-
);
16+
]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
if (! $validation->run($data)) {
4+
// handle validation errors
5+
}
6+
// or
7+
if (! $validation->run($data, 'signup')) {
8+
// handle validation errors
9+
}

0 commit comments

Comments
 (0)