From 55722a5db40908b65ab477283425d57af0236eaf Mon Sep 17 00:00:00 2001 From: October CMS Date: Sat, 29 Jul 2023 15:54:23 +1000 Subject: [PATCH] Adds index/position support to error messages --- src/Validation/Concerns/FormatsMessages.php | 24 +++++++++++++++------ src/Validation/Validator.php | 4 +++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Validation/Concerns/FormatsMessages.php b/src/Validation/Concerns/FormatsMessages.php index f8a0c6c68..ff52f7b7b 100644 --- a/src/Validation/Concerns/FormatsMessages.php +++ b/src/Validation/Concerns/FormatsMessages.php @@ -3,7 +3,9 @@ use Illuminate\Support\Str; /** - * FormatsMessages is a modifier to the base trait + * FormatsMessages is a modifier to the base trait, it implements + * some extended technology to allow rule objects to specify methods + * message() and replace() for custom messaging. * * @see \Illuminate\Validation\Concerns\FormatsMessages */ @@ -17,6 +19,10 @@ trait FormatsMessages */ protected function getMessage($attribute, $rule) { + $attributeWithPlaceholders = $attribute; + + $attribute = $this->replacePlaceholderInString($attribute); + $inlineMessage = $this->getInlineMessage($attribute, $rule); // First we will retrieve the custom message for the validation rule if one @@ -28,8 +34,12 @@ protected function getMessage($attribute, $rule) $lowerRule = Str::snake($rule); + $customKey = "validation.custom.{$attribute}.{$lowerRule}"; + $customMessage = $this->getCustomMessageFromTranslator( - $customKey = "validation.custom.{$attribute}.{$lowerRule}" + in_array($rule, $this->sizeRules) + ? [$customKey.".{$this->getAttributeType($attribute)}", $customKey] + : $customKey ); // First we check for a custom defined validation message for the attribute @@ -39,8 +49,7 @@ protected function getMessage($attribute, $rule) return $customMessage; } - // Apply fallback message from extension class, if one exists. - // This is custom logic from the parent class. + // Modification: Apply fallback message from extension class, if one exists. if ($this->hasExtensionMethod($lowerRule, 'message')) { return $this->callExtensionMethod($lowerRule, 'message'); } @@ -49,7 +58,7 @@ protected function getMessage($attribute, $rule) // specific error message for the type of attribute being validated such // as a number, file or string which all have different message types. if (in_array($rule, $this->sizeRules)) { - return $this->getSizeMessage($attribute, $rule); + return $this->getSizeMessage($attributeWithPlaceholders, $rule); } // Finally, if no developer specified messages have been set, and no other @@ -85,6 +94,8 @@ public function makeReplacements($message, $attribute, $rule, $parameters) $lowerRule = Str::snake($rule); $message = $this->replaceInputPlaceholder($message, $attribute); + $message = $this->replaceIndexPlaceholder($message, $attribute); + $message = $this->replacePositionPlaceholder($message, $attribute); if (isset($this->replacers[$lowerRule])) { return $this->callReplacer($message, $attribute, $lowerRule, $parameters, $this); @@ -93,8 +104,7 @@ public function makeReplacements($message, $attribute, $rule, $parameters) return $this->$replacer($message, $attribute, $rule, $parameters); } - // Apply fallback replacer from extension class, if one exists. - // This is custom logic from the parent class. + // Modification: Apply fallback replacer from extension class, if one exists. if ($this->hasExtensionMethod($lowerRule, 'replace')) { return $this->callExtensionMethod($lowerRule, 'replace', [$message, $attribute, $lowerRule, $parameters]); } diff --git a/src/Validation/Validator.php b/src/Validation/Validator.php index a15799f4b..5bba58180 100644 --- a/src/Validation/Validator.php +++ b/src/Validation/Validator.php @@ -4,7 +4,9 @@ use October\Rain\Exception\ValidationException; /** - * Validator is a modifier to the base class + * Validator is a modifier to the base class, it extends validation rules + * with extra methods for specifying messages and replacements inside the + * class definition. */ class Validator extends ValidatorBase {