@@ -579,15 +579,26 @@ right after the name of the field the error should belong to::
579579Creating Custom Rules
580580*********************
581581
582+ Using Rule Classes
583+ ==================
584+
582585Rules 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
584592add the new file to the ``$ruleSets `` array:
585593
586594.. literalinclude :: validation/033.php
587595
588596You can add it as either a simple string with the fully qualified class name, or using the ``::class `` suffix as
589597shown 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+
591602Within the file itself, each method is a rule and must accept a string as the first parameter, and must return
592603a 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+
602616Your new custom rule could now be used just like any other rule:
603617
604618.. literalinclude :: validation/036.php
605619
606620Allowing Parameters
607- ===================
621+ -------------------
608622
609623If your method needs to work with parameters, the function will need a minimum of three parameters: the string to validate,
610624the 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
615629Custom 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+
617648Available Rules
618649***************
619650
0 commit comments