Skip to content

Commit

Permalink
Adds index/position support to error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
octoberapp committed Jul 29, 2023
1 parent 5769998 commit 55722a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
24 changes: 17 additions & 7 deletions src/Validation/Concerns/FormatsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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');
}
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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]);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 55722a5

Please sign in to comment.