From f24c53aa00e4db7b4124428dd8e705af59b8ba10 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 8 Nov 2023 16:56:08 +0900 Subject: [PATCH] feat: improve error key --- system/Validation/Validation.php | 8 +++++++- tests/system/Validation/RulesTest.php | 3 +-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/system/Validation/Validation.php b/system/Validation/Validation.php index b03badb9bf43..81f52717e4bf 100644 --- a/system/Validation/Validation.php +++ b/system/Validation/Validation.php @@ -356,8 +356,14 @@ protected function processRules( $param = ($param === false) ? '' : $param; + if ($rule === 'field_exists') { + $fieldForErrors = $originalField; + } else { + $fieldForErrors = $field; + } + // @phpstan-ignore-next-line $error may be set by rule methods. - $this->errors[$field] = $error ?? $this->getErrorMessage( + $this->errors[$fieldForErrors] = $error ?? $this->getErrorMessage( ($this->isClosure($rule) || $arrayCallable) ? (string) $i : $rule, $field, $label, diff --git a/tests/system/Validation/RulesTest.php b/tests/system/Validation/RulesTest.php index 844daab3f005..4844d898e3a9 100644 --- a/tests/system/Validation/RulesTest.php +++ b/tests/system/Validation/RulesTest.php @@ -948,8 +948,7 @@ public function testFieldExistsErrorMessage(): void $this->assertFalse($this->validation->run($data)); $this->assertSame( - // This errror message is not perfect. - ['fiz.bar.baz' => 'The fiz.*.baz field must exist.'], + ['fiz.*.baz' => 'The fiz.*.baz field must exist.'], $this->validation->getErrors() ); }