8000 [9.x] Fix array to string conversion when using custom validation messages for size rules by mateusjunges · Pull Request #40074 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[9.x] Fix array to string conversion when using custom validation messages for size rules #40074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 16, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
formatting
  • Loading branch information
taylorotwell committed Dec 16, 2021
commit 128fcf8451afa690446748da296629e9fbee5016
46 changes: 28 additions & 18 deletions src/Illuminate/Validation/Concerns/FormatsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ protected function getMessage($attribute, $rule)

$lowerRule = Str::snake($rule);

$customKey = in_array($rule, $this->sizeRules)
? "validation.custom.{$attribute}.{$lowerRule}.{$this->getAttributeType($attribute)}"
: "validation.custom.{$attribute}.{$lowerRule}";
$customKey = "validation.custom.{$attribute}.{$lowerRule}";

$customMessage = $this->getCustomMessageFromTranslator($customKey);
$customMessage = $this->getCustomMessageFromTranslator(
in_array($rule, $this->sizeRules)
? [$customKey.".{$this->getAttributeType($attribute)}", $customKey]
: $customKey
);

// First we check for a custom defined validation message for the attribute
// and rule. This allows the developer to specify specific messages for
Expand Down Expand Up @@ -120,25 +122,33 @@ protected function getFromLocalArray($attribute, $lowerRule, $source = null)
/**
* Get the custom error message from the translator.
*
* @param string $key
* @param array|string $keys
* @return string
*/
protected function getCustomMessageFromTranslator($key)
protected function getCustomMessageFromTranslator($keys)
{
if (($message = $this->translator->get($key)) !== $key) {
return $message;
}
foreach (Arr::wrap($keys) as $key) {
if (($message = $this->translator->get($key)) !== $key) {
return $message;
}

// If an exact match was not found for the key, we will collapse all of these
// messages and loop through them and try to find a wildcard match for the
// given key. Otherwise, we will simply return the key's value back out.
$shortKey = preg_replace(
'/^validation\.custom\./', '', $key
);
// If an exact match was not found for the key, we will collapse all of these
// messages and loop through them and try to find a wildcard match for the
// given key. Otherwise, we will simply return the key's value back out.
$shortKey = preg_replace(
'/^validation\.custom\./', '', $key
);

$message = $this->getWildcardCustomMessages(Arr::dot(
(array) $this->translator->get('validation.custom')
), $shortKey, $key);

if ($message !== $key) {
return $message;
}
}

return $this->getWildcardCustomMessages(Arr::dot(
(array) $this->translator->get('validation.custom')
), $shortKey, $key);
return Arr::last(Arr::wrap($keys));
}

/**
Expand Down
0