Skip to content

Commit 8d38d1f

Browse files
committed
docs: add docs
1 parent 661dfee commit 8d38d1f

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

user_guide_src/source/libraries/validation.rst

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,15 +579,26 @@ right after the name of the field the error should belong to::
579579
Creating Custom Rules
580580
*********************
581581

582+
Using Rule Classes
583+
==================
584+
582585
Rules are stored within simple, namespaced classes. They can be stored any location you would like, as long as the
583-
autoloader can find it. These files are called RuleSets. To add a new RuleSet, edit **Config/Validation.php** and
586+
autoloader can find it. These files are called RuleSets.
587+
588+
Adding a RuleSet
589+
----------------
590+
591+
To add a new RuleSet, edit **Config/Validation.php** and
584592
add the new file to the ``$ruleSets`` array:
585593

586594
.. literalinclude:: validation/033.php
587595

588596
You can add it as either a simple string with the fully qualified class name, or using the ``::class`` suffix as
589597
shown above. The primary benefit here is that it provides some extra navigation capabilities in more advanced IDEs.
590598

599+
Creating a Rule Class
600+
---------------------
601+
591602
Within the file itself, each method is a rule and must accept a string as the first parameter, and must return
592603
a boolean true or false value signifying true if it passed the test or false if it did not:
593604

@@ -599,12 +610,15 @@ second parameter:
599610

600611
.. literalinclude:: validation/035.php
601612

613+
Using a Custom Rule
614+
-------------------
615+
602616
Your new custom rule could now be used just like any other rule:
603617

604618
.. literalinclude:: validation/036.php
605619

606620
Allowing Parameters
607-
===================
621+
-------------------
608622

609623
If your method needs to work with parameters, the function will need a minimum of three parameters: the string to validate,
610624
the parameter string, and an array with all of the data that was submitted the form. The ``$data`` array is especially handy
@@ -614,6 +628,23 @@ for rules like ``required_with`` that needs to check the value of another submit
614628

615629
Custom errors can be returned as the fourth parameter, just as described above.
616630

631+
.. _validation-using-closure-rule:
632+
633+
Using Closure Rule
634+
==================
635+
636+
.. versionadded:: 4.3.0
637+
638+
If you only need the functionality of a custom rule once throughout your application,
639+
you may use a closure instead of a rule class.
640+
641+
You need to use an array for validation rules:
642+
643+
.. literalinclude:: validation/040.php
644+
645+
When you specify the error message, set the array key for the closure rule.
646+
In the above code, the ``required`` rule has the key ``0``, and the closure has ``1``.
647+
617648
Available Rules
618649
***************
619650

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
$validation->setRules(
4+
[
5+
'foo' => [
6+
'required',
7+
static fn ($value) => (int) $value % 2 === 0,
8+
],
9+
],
10+
[
11+
// Errors
12+
'foo' => [
13+
// Specify the array key for the closure rule.
14+
1 => 'The value is not even.',
15+
],
16+
],
17+
);
18+
19+
if (! $validation->run($data)) {
20+
// handle validation errors
21+
}

0 commit comments

Comments
 (0)