Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 27 additions & 15 deletions src/Illuminate/Validation/Concerns/FormatsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,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 Down Expand Up @@ -118,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
25 changes: 25 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,31 @@ public function testCustomValidationLinesAreRespected()
$this->assertSame('really required!', $v->messages()->first('name'));
}

public function testCustomValidationLinesForSizeRules()
{
$trans = $this->getIlluminateArrayTranslator();
$trans->getLoader()->addMessages('en', 'validation', [
'required' => 'required!',
'custom' => [
'image' => [
'gte' => [
'file' => 'Custom message for image files.',
'string' => 'Custom message for image filenames.',
],
],
],
]);

$v = new Validator($trans, ['image' => 'image.png'], ['image' => 'gte:50']);
$this->assertFalse($v->passes());
$this->assertSame('Custom message for image filenames.', $v->messages()->first('image'));

$file = new UploadedFile(__FILE__, '', null, null, true);
$v = new Validator($trans, ['image' => $file], ['image' => 'gte:50']);
$this->assertFalse($v->passes());
$this->assertSame('Custom message for image files.', $v->messages()->first('image'));
}

public function testCustomValidationLinesAreRespectedWithAsterisks()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down