Skip to content

Commit 0798239

Browse files
authored
Merge pull request #7549 from kenjis/fix-validation-placeholder-DBGroup
fix: [Validation] DBGroup is ignored when checking the value of a placeholder
2 parents fa86c55 + a411369 commit 0798239

File tree

5 files changed

+69
-10
lines changed

5 files changed

+69
-10
lines changed

system/Validation/Validation.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,15 @@ public function __construct($config, RendererInterface $view)
116116
* @param array|null $data The array of data to validate.
117117
* @param string|null $group The predefined group of rules to apply.
118118
* @param string|null $dbGroup The database group to use.
119+
*
120+
* @TODO Type ?string for $dbGroup should be removed.
121+
* See https://github.com/codeigniter4/CodeIgniter4/issues/6723
119122
*/
120123
public function run(?array $data = null, ?string $group = null, ?string $dbGroup = null): bool
121124
{
122125
$data ??= $this->data;
123126

124-
// i.e. is_unique
127+
// `DBGroup` is a reserved name. For is_unique and is_not_unique
125128
$data['DBGroup'] = $dbGroup;
126129

127130
$this->loadRuleSets();
@@ -184,17 +187,28 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
184187
}
185188

186189
/**
187-
* Runs the validation process, returning true or false
188-
* determining whether validation was successful or not.
190+
* Runs the validation process, returning true or false determining whether
191+
* validation was successful or not.
189192
*
190193
* @param array|bool|float|int|object|string|null $value
194+
* @param array|string $rules
191195
* @param string[] $errors
196+
* @param string|null $dbGroup The database group to use.
192197
*/
193-
public function check($value, string $rule, array $errors = []): bool
198+
public function check($value, $rules, array $errors = [], $dbGroup = null): bool
194199
{
195200
$this->reset();
196201

197-
return $this->setRule('check', null, $rule, $errors)->run(['check' => $value]);
202+
return $this->setRule(
203+
'check',
204+
null,
205+
$rules,
206+
$errors
207+
)->run(
208+
['check' => $value],
209+
null,
210+
$dbGroup
211+
);
198212
}
199213

200214
/**
@@ -679,6 +693,7 @@ protected function fillPlaceholders(array $rules, array $data): array
679693

680694
foreach ($placeholderFields as $field) {
681695
$validator ??= Services::validation(null, false);
696+
assert($validator instanceof Validation);
682697

683698
$placeholderRules = $rules[$field]['rules'] ?? null;
684699

@@ -699,7 +714,8 @@ protected function fillPlaceholders(array $rules, array $data): array
699714
}
700715

701716
// Validate the placeholder field
702-
if (! $validator->check($data[$field], implode('|', $placeholderRules))) {
717+
$dbGroup = $data['DBGroup'] ?? null;
718+
if (! $validator->check($data[$field], $placeholderRules, [], $dbGroup)) {
703719
// if fails, do nothing
704720
continue;
705721
}

system/Validation/ValidationInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
3232
* Check; runs the validation process, returning true or false
3333
* determining whether or not validation was successful.
3434
*
35-
* @param array|bool|float|int|object|string|null $value Value to validate.
35+
* @param array|bool|float|int|object|string|null $value Value to validate.
36+
* @param array|string $rules
3637
* @param string[] $errors
38+
* @param string|null $dbGroup The database group to use.
3739
*
3840
* @return bool True if valid, else false.
3941
*/
40-
public function check($value, string $rule, array $errors = []): bool;
42+
public function check($value, $rules, array $errors = [], $dbGroup = null): bool;
4143

4244
/**
4345
* Takes a Request object and grabs the input data to use from its

tests/system/Validation/StrictRules/DatabaseRelatedRulesTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\Validation\Validation;
1717
use Config\Database;
1818
use Config\Services;
19+
use InvalidArgumentException;
1920
use Tests\Support\Validation\TestRules;
2021

2122
/**
@@ -82,7 +83,17 @@ public function testIsUniqueTrue(): void
8283
$this->assertTrue($this->validation->run($data));
8384
}
8485

85-
public function testIsUniqueIgnoresParams(): void
86+
public function testIsUniqueWithInvalidDBGroup(): void
87+
{
88+
$this->expectException(InvalidArgumentException::class);
89+
$this->expectExceptionMessage('invalidGroup is not a valid database connection group');
90+
91+
$this->validation->setRules(['email' => 'is_unique[user.email]']);
92+
$data = ['email' => '[email protected]'];
93+
$this->assertTrue($this->validation->run($data, null, 'invalidGroup'));
94+
}
95+
96+
public function testIsUniqueWithIgnoreValue(): void
8697
{
8798
$db = Database::connect();
8899
$db
@@ -102,7 +113,7 @@ public function testIsUniqueIgnoresParams(): void
102113
$this->assertTrue($this->validation->run($data));
103114
}
104115

105-
public function testIsUniqueIgnoresParamsPlaceholders(): void
116+
public function testIsUniqueWithIgnoreValuePlaceholder(): void
106117
{
107118
$this->hasInDatabase('user', [
108119
'name' => 'Derek',

user_guide_src/source/changelogs/v4.3.6.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ Release Date: Unreleased
1212
BREAKING
1313
********
1414

15+
Interface Changes
16+
=================
17+
18+
.. note:: As long as you have not extended the relevant CodeIgniter core classes
19+
or implemented these interfaces, all these changes are backward compatible
20+
and require no intervention.
21+
22+
ValidationInterface::check()
23+
----------------------------
24+
25+
- The second parameter has changed from ``string $rule`` to ``$rules``.
26+
- The optional fourth parameter ``$dbGroup = null`` has been added.
27+
28+
Method Signature Changes
29+
========================
30+
31+
Validation::check()
32+
-------------------
33+
34+
- The second parameter has changed from ``string $rule`` to ``$rules``.
35+
- The optional fourth parameter ``$dbGroup = null`` has been added.
36+
1537
Message Changes
1638
***************
1739

@@ -24,6 +46,11 @@ Deprecations
2446
Bugs Fixed
2547
**********
2648

49+
- **Validation:** Fixed a bug that ``$DBGroup`` is ignored when checking
50+
the value of a placeholder.
51+
- **Validation:** Fixed a bug that ``check()`` cannot specify non-default
52+
database group.
53+
2754
See the repo's
2855
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
2956
for a complete list of bugs fixed.

user_guide_src/source/installation/upgrade_436.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Breaking Changes
2121
Breaking Enhancements
2222
*********************
2323

24+
- The method signatures of ``ValidationInterface::check()`` and ``Validation::check()``
25+
have been changed. If you implement or extend them, update the signatures.
26+
2427
Project Files
2528
*************
2629

0 commit comments

Comments
 (0)