Skip to content

Commit 44c0c19

Browse files
committed
fix: validation error when a closure is used in combination with permit_empty or if_exist rules
1 parent 42b174f commit 44c0c19

4 files changed

Lines changed: 23 additions & 3 deletions

File tree

system/Validation/Validation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ protected function processRules(
239239
}
240240

241241
// Otherwise remove the if_exist rule and continue the process
242-
$rules = array_diff($rules, ['if_exist']);
242+
$rules = array_filter($rules, static fn ($rule) => $rule instanceof Closure || $rule !== 'if_exist');
243243
}
244244

245245
if (in_array('permit_empty', $rules, true)) {
@@ -250,7 +250,7 @@ protected function processRules(
250250
$passed = true;
251251

252252
foreach ($rules as $rule) {
253-
if (preg_match('/(.*?)\[(.*)\]/', $rule, $match)) {
253+
if (! $this->isClosure($rule) && preg_match('/(.*?)\[(.*)\]/', $rule, $match)) {
254254
$rule = $match[1];
255255
$param = $match[2];
256256

@@ -275,7 +275,7 @@ protected function processRules(
275275
}
276276
}
277277

278-
$rules = array_diff($rules, ['permit_empty']);
278+
$rules = array_filter($rules, static fn ($rule) => $rule instanceof Closure || $rule !== 'permit_empty');
279279
}
280280

281281
foreach ($rules as $i => $rule) {

tests/system/Validation/RulesTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ public function ifExistProvider(): Generator
117117
['foo' => []],
118118
true,
119119
],
120+
// Testing with closure
121+
[
122+
['foo' => ['if_exist', static fn ($value) => true]],
123+
['foo' => []],
124+
true,
125+
],
120126
];
121127
}
122128

@@ -275,6 +281,12 @@ public function providePermitEmptyCases(): Generator
275281
['foo' => '', 'bar' => 1],
276282
true,
277283
],
284+
// Testing with closure
285+
[
286+
['foo' => ['permit_empty', static fn ($value) => true]],
287+
['foo' => ''],
288+
true,
289+
],
278290
];
279291
}
280292

tests/system/Validation/StrictRules/RulesTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ public function providePermitEmptyCasesStrict(): Generator
8484
['foo' => false],
8585
true,
8686
],
87+
// Testing with closure
88+
[
89+
['foo' => ['permit_empty', static fn ($value) => true]],
90+
['foo' => ''],
91+
true,
92+
],
8793
];
8894
}
8995

user_guide_src/source/changelogs/v4.3.5.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Deprecations
2424
Bugs Fixed
2525
**********
2626

27+
- **Validation:** Fixed a bug where a closure used in combination with ``permit_empty`` or ``if_exist`` rules was causing an error.
28+
2729
See the repo's
2830
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
2931
for a complete list of bugs fixed.

0 commit comments

Comments
 (0)