Skip to content

Commit 8785776

Browse files
authored
Merge pull request #6253 from kenjis/fix-ValidationInterface
fix: ValidationInterface
2 parents 3b4ba87 + c332ee6 commit 8785776

File tree

9 files changed

+121
-22
lines changed

9 files changed

+121
-22
lines changed

phpstan-baseline.neon.dist

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -635,11 +635,6 @@ parameters:
635635
count: 1
636636
path: system/Throttle/Throttler.php
637637

638-
-
639-
message: "#^Variable \\$error on left side of \\?\\? always exists and is always null\\.$#"
640-
count: 1
641-
path: system/Validation/Validation.php
642-
643638
-
644639
message: "#^Property CodeIgniter\\\\View\\\\Cell\\:\\:\\$cache \\(CodeIgniter\\\\Cache\\\\CacheInterface\\) in empty\\(\\) is not falsy\\.$#"
645640
count: 2

system/BaseModel.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use CodeIgniter\Exceptions\ModelException;
2020
use CodeIgniter\I18n\Time;
2121
use CodeIgniter\Pager\Pager;
22-
use CodeIgniter\Validation\Validation;
2322
use CodeIgniter\Validation\ValidationInterface;
2423
use Config\Services;
2524
use InvalidArgumentException;
@@ -198,7 +197,7 @@ abstract class BaseModel
198197
/**
199198
* Our validator instance.
200199
*
201-
* @var Validation
200+
* @var ValidationInterface
202201
*/
203202
protected $validation;
204203

@@ -326,7 +325,7 @@ public function __construct(?ValidationInterface $validation = null)
326325
$this->tempAllowCallbacks = $this->allowCallbacks;
327326

328327
/**
329-
* @var Validation|null $validation
328+
* @var ValidationInterface|null $validation
330329
*/
331330
$validation ??= Services::validation(null, false);
332331
$this->validation = $validation;

system/Config/BaseService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
use CodeIgniter\Session\Session;
5050
use CodeIgniter\Throttle\Throttler;
5151
use CodeIgniter\Typography\Typography;
52-
use CodeIgniter\Validation\Validation;
52+
use CodeIgniter\Validation\ValidationInterface;
5353
use CodeIgniter\View\Cell;
5454
use CodeIgniter\View\Parser;
5555
use CodeIgniter\View\RendererInterface;
@@ -127,7 +127,7 @@
127127
* @method static Toolbar toolbar(ConfigToolbar $config = null, $getShared = true)
128128
* @method static Typography typography($getShared = true)
129129
* @method static URI uri($uri = null, $getShared = true)
130-
* @method static Validation validation(ConfigValidation $config = null, $getShared = true)
130+
* @method static ValidationInterface validation(ConfigValidation $config = null, $getShared = true)
131131
* @method static Cell viewcell($getShared = true)
132132
*/
133133
class BaseService

system/Config/Services.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
use CodeIgniter\Throttle\Throttler;
5555
use CodeIgniter\Typography\Typography;
5656
use CodeIgniter\Validation\Validation;
57+
use CodeIgniter\Validation\ValidationInterface;
5758
use CodeIgniter\View\Cell;
5859
use CodeIgniter\View\Parser;
5960
use CodeIgniter\View\RendererInterface;
@@ -733,7 +734,7 @@ public static function uri(?string $uri = null, bool $getShared = true)
733734
/**
734735
* The Validation class provides tools for validating input data.
735736
*
736-
* @return Validation
737+
* @return ValidationInterface
737738
*/
738739
public static function validation(?ValidationConfig $config = null, bool $getShared = true)
739740
{

system/Controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use CodeIgniter\HTTP\RequestInterface;
1616
use CodeIgniter\HTTP\ResponseInterface;
1717
use CodeIgniter\Validation\Exceptions\ValidationException;
18-
use CodeIgniter\Validation\Validation;
18+
use CodeIgniter\Validation\ValidationInterface;
1919
use Config\Services;
2020
use Psr\Log\LoggerInterface;
2121

@@ -62,7 +62,7 @@ class Controller
6262
/**
6363
* Once validation has been run, will hold the Validation instance.
6464
*
65-
* @var Validation
65+
* @var ValidationInterface
6666
*/
6767
protected $validator;
6868

system/Validation/Validation.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ protected function processRules(
331331

332332
$param = ($param === false) ? '' : $param;
333333

334+
// @phpstan-ignore-next-line $error may be set by rule methods.
334335
$this->errors[$field] = $error ?? $this->getErrorMessage(
335336
$rule,
336337
$field,
@@ -405,7 +406,7 @@ public function withRequest(RequestInterface $request): ValidationInterface
405406
*
406407
* [
407408
* 'rule' => 'message',
408-
* 'rule' => 'message'
409+
* 'rule' => 'message',
409410
* ]
410411
*
411412
* @param array|string $rules
@@ -499,7 +500,7 @@ public function hasRule(string $field): bool
499500
*
500501
* @param string $group Group.
501502
*
502-
* @throws InvalidArgumentException If group not found.
503+
* @throws ValidationException If group not found.
503504
*
504505
* @return string[] Rule group.
505506
*/
@@ -521,7 +522,7 @@ public function getRuleGroup(string $group): array
521522
*
522523
* @param string $group Group.
523524
*
524-
* @throws InvalidArgumentException If group not found.
525+
* @throws ValidationException If group not found.
525526
*/
526527
public function setRuleGroup(string $group)
527528
{
@@ -593,12 +594,14 @@ protected function loadRuleSets()
593594
* same format used with setRules(). Additionally, check
594595
* for {group}_errors for an array of custom error messages.
595596
*
596-
* @return array|ValidationException|null
597+
* @throws ValidationException
598+
*
599+
* @return array
597600
*/
598601
public function loadRuleGroup(?string $group = null)
599602
{
600603
if (empty($group)) {
601-
return null;
604+
return [];
602605
}
603606

604607
if (! isset($this->config->{$group})) {

system/Validation/ValidationInterface.php

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ interface ValidationInterface
2020
{
2121
/**
2222
* Runs the validation process, returning true/false determining whether
23-
* or not validation was successful.
23+
* validation was successful or not.
2424
*
25-
* @param array $data The array of data to validate.
26-
* @param string $group The pre-defined group of rules to apply.
25+
* @param array|null $data The array of data to validate.
26+
* @param string|null $group The predefined group of rules to apply.
27+
* @param string|null $dbGroup The database group to use.
2728
*/
28-
public function run(?array $data = null, ?string $group = null): bool;
29+
public function run(?array $data = null, ?string $group = null, ?string $dbGroup = null): bool;
2930

3031
/**
3132
* Check; runs the validation process, returning true or false
@@ -45,16 +46,54 @@ public function check($value, string $rule, array $errors = []): bool;
4546
*/
4647
public function withRequest(RequestInterface $request): ValidationInterface;
4748

49+
/**
50+
* Sets an individual rule and custom error messages for a single field.
51+
*
52+
* The custom error message should be just the messages that apply to
53+
* this field, like so:
54+
*
55+
* [
56+
* 'rule' => 'message',
57+
* 'rule' => 'message',
58+
* ]
59+
*
60+
* @param array|string $rules
61+
*
62+
* @return $this
63+
*/
64+
public function setRule(string $field, ?string $label, $rules, array $errors = []);
65+
4866
/**
4967
* Stores the rules that should be used to validate the items.
5068
*/
5169
public function setRules(array $rules, array $messages = []): ValidationInterface;
5270

71+
/**
72+
* Returns all of the rules currently defined.
73+
*/
74+
public function getRules(): array;
75+
5376
/**
5477
* Checks to see if the rule for key $field has been set or not.
5578
*/
5679
public function hasRule(string $field): bool;
5780

81+
/**
82+
* Get rule group.
83+
*
84+
* @param string $group Group.
85+
*
86+
* @return string[] Rule group.
87+
*/
88+
public function getRuleGroup(string $group): array;
89+
90+
/**
91+
* Set rule group.
92+
*
93+
* @param string $group Group.
94+
*/
95+
public function setRuleGroup(string $group);
96+
5897
/**
5998
* Returns the error for a specified $field (or empty string if not set).
6099
*/
@@ -83,4 +122,31 @@ public function setError(string $alias, string $error): ValidationInterface;
83122
* you need to process more than one array.
84123
*/
85124
public function reset(): ValidationInterface;
125+
126+
/**
127+
* Loads custom rule groups (if set) into the current rules.
128+
*
129+
* Rules can be pre-defined in Config\Validation and can
130+
* be any name, but must all still be an array of the
131+
* same format used with setRules(). Additionally, check
132+
* for {group}_errors for an array of custom error messages.
133+
*
134+
* @return array
135+
*/
136+
public function loadRuleGroup(?string $group = null);
137+
138+
/**
139+
* Checks to see if an error exists for the given field.
140+
*/
141+
public function hasError(string $field): bool;
142+
143+
/**
144+
* Returns the rendered HTML of the errors as defined in $template.
145+
*/
146+
public function listErrors(string $template = 'list'): string;
147+
148+
/**
149+
* Displays a single error in formatted HTML as defined in the $template view.
150+
*/
151+
public function showError(string $field, string $template = 'single'): string;
86152
}

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,35 @@ For example, the Exit code has been changed like the following:
3737
- If an uncaught ``CastException`` occurs, the Exit code is ``EXIT_CONFIG`` (= ``3``) instead of ``9``.
3838
- If an uncaught ``DatabaseException`` occurs, the Exit code is ``EXIT_DATABASE`` (= ``8``) instead of ``17``.
3939

40+
Method Signature Changes
41+
========================
42+
43+
.. _v430_validation_changes:
44+
45+
Validation Changes
46+
------------------
47+
48+
ValidationInterface
49+
^^^^^^^^^^^^^^^^^^^
50+
51+
``ValidationInterface`` has been changed to eliminate the mismatch between ``ValidationInterface`` and the ``Validation`` class.
52+
53+
- The third parameter ``$dbGroup`` for ``ValidationInterface::run()`` has been added.
54+
- The following methods are added to the interface:
55+
- ``ValidationInterface::setRule()``
56+
- ``ValidationInterface::getRules()``
57+
- ``ValidationInterface::getRuleGroup()``
58+
- ``ValidationInterface::setRuleGroup()``
59+
- ``ValidationInterface::loadRuleGroup()``
60+
- ``ValidationInterface::hasError()``
61+
- ``ValidationInterface::listErrors()``
62+
- ``ValidationInterface::showError()``
63+
64+
Validation
65+
^^^^^^^^^^
66+
67+
The return value of ``Validation::loadRuleGroup()`` has been changed ``null`` to ``[]`` when the ``$group`` is empty.
68+
4069
Others
4170
------
4271

user_guide_src/source/installation/upgrade_430.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ If you have code that depends on the bug, you need to change the code.
7474
Use new Form helpers, :php:func:`validation_errors()`, :php:func:`validation_list_errors()` and :php:func:`validation_show_error()` to display Validation Errors,
7575
instead of the Validation object.
7676

77+
Validation Changes
78+
==================
79+
80+
- ``ValidationInterface`` has been changed. Implemented classes should likewise add the methods and the parameters so as not to break LSP. See :ref:`v430_validation_changes` for details.
81+
- The return value of ``Validation::loadRuleGroup()`` has been changed ``null`` to ``[]`` when the ``$group`` is empty. Update the code if you depend on the behavior.
82+
7783
.. _upgrade-430-stream-filter:
7884

7985
Capturing STDERR and STDOUT streams in tests

0 commit comments

Comments
 (0)