Skip to content

Commit 368ab7f

Browse files
committed
feat: add closure parameters
1 parent b17710e commit 368ab7f

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

system/Validation/Validation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ protected function processRules(
293293
$error = null;
294294

295295
// If it's a callable, call and get out of here.
296-
if ($isCallable) {
296+
if ($this->isClosure($rule)) {
297+
$passed = $rule($value, $data, $error, $field);
298+
} elseif ($isCallable) {
297299
$passed = $param === false ? $rule($value) : $rule($value, $param, $data);
298300
} else {
299301
$found = false;

tests/system/Validation/ValidationTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,35 @@ public function testClosureRule(): void
229229
);
230230
}
231231

232+
public function testClosureRuleWithParamError(): void
233+
{
234+
$this->validation->setRules(
235+
[
236+
'foo' => [
237+
'required',
238+
static function ($value, $data, &$error, $field) {
239+
if ($value !== 'abc') {
240+
$error = 'The ' . $field . ' value is not "abc"';
241+
242+
return false;
243+
}
244+
245+
return true;
246+
},
247+
],
248+
],
249+
);
250+
251+
$data = ['foo' => 'xyz'];
252+
$return = $this->validation->run($data);
253+
254+
$this->assertFalse($return);
255+
$this->assertSame(
256+
['foo' => 'The foo value is not "abc"'],
257+
$this->validation->getErrors()
258+
);
259+
}
260+
232261
/**
233262
* @see https://github.com/codeigniter4/CodeIgniter4/issues/5368
234263
*

0 commit comments

Comments
 (0)