From 8535eaa315d6321affc024d2315ac54cc0e4650f Mon Sep 17 00:00:00 2001 From: marcusorjames Date: Mon, 26 Aug 2024 18:42:34 +0100 Subject: [PATCH 1/2] update(callback): Use rule key as rule name (#1) This allows for multiple callback rules per item and also indexes the `ErrorBag`, which is helpful for identifying what validation failures exist. --- src/Validation.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Validation.php b/src/Validation.php index d74786a..e523b79 100644 --- a/src/Validation.php +++ b/src/Validation.php @@ -489,13 +489,16 @@ protected function resolveRules($rules): array } $params = []; + if (is_string($rule)) { list($rulename, $params) = $this->parseRule($rule); $validator = call_user_func_array($validatorFactory, array_merge([$rulename], $params)); } elseif ($rule instanceof Rule) { $validator = $rule; } elseif ($rule instanceof Closure) { - $validator = call_user_func_array($validatorFactory, ['callback', $rule]); + $ruleName = is_string($i) ? $i : 'callback'; + $validator = call_user_func_array($validatorFactory, ['callback', $rule ]); + $validator->setKey($ruleName); } else { $ruleName = is_object($rule) ? get_class($rule) : gettype($rule); $message = "Rule must be a string, Closure or '".Rule::class."' instance. ".$ruleName." given"; From bc41eb90571608b428522fb54225ea26dc27a6ec Mon Sep 17 00:00:00 2001 From: marcusorjames Date: Mon, 26 Aug 2024 18:43:18 +0100 Subject: [PATCH 2/2] feature(rule): Allow public access to validation array (#2) This is specifically useful for callbacks where Rule is bound to the closure. As private/protected properties are not accessible in bound closures. --- src/Rule.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Rule.php b/src/Rule.php index 16d0e94..3bff599 100644 --- a/src/Rule.php +++ b/src/Rule.php @@ -43,6 +43,11 @@ public function setValidation(Validation $validation) $this->validation = $validation; } + public function getValidation() + { + return $this->validation; + } + /** * Set key *