Skip to content

Commit

Permalink
Merge pull request #29716 from rgehan/fix-29441
Browse files Browse the repository at this point in the history
[5.8] Use custom attributes in lt/lte/gt/gte rules messages
  • Loading branch information
taylorotwell committed Aug 24, 2019
2 parents b97edfc + 8999c4d commit 7725e7a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Illuminate/Validation/Concerns/ReplacesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ protected function replaceSize($message, $attribute, $rule, $parameters)
protected function replaceGt($message, $attribute, $rule, $parameters)
{
if (is_null($value = $this->getValue($parameters[0]))) {
return str_replace(':value', $parameters[0], $message);
return str_replace(':value', $this->getDisplayableAttribute($parameters[0]), $message);
}

return str_replace(':value', $this->getSize($attribute, $value), $message);
Expand All @@ -278,7 +278,7 @@ protected function replaceGt($message, $attribute, $rule, $parameters)
protected function replaceLt($message, $attribute, $rule, $parameters)
{
if (is_null($value = $this->getValue($parameters[0]))) {
return str_replace(':value', $parameters[0], $message);
return str_replace(':value', $this->getDisplayableAttribute($parameters[0]), $message);
}

return str_replace(':value', $this->getSize($attribute, $value), $message);
Expand All @@ -296,7 +296,7 @@ protected function replaceLt($message, $attribute, $rule, $parameters)
protected function replaceGte($message, $attribute, $rule, $parameters)
{
if (is_null($value = $this->getValue($parameters[0]))) {
return str_replace(':value', $parameters[0], $message);
return str_replace(':value', $this->getDisplayableAttribute($parameters[0]), $message);
}

return str_replace(':value', $this->getSize($attribute, $value), $message);
Expand All @@ -314,7 +314,7 @@ protected function replaceGte($message, $attribute, $rule, $parameters)
protected function replaceLte($message, $attribute, $rule, $parameters)
{
if (is_null($value = $this->getValue($parameters[0]))) {
return str_replace(':value', $parameters[0], $message);
return str_replace(':value', $this->getDisplayableAttribute($parameters[0]), $message);
}

return str_replace(':value', $this->getSize($attribute, $value), $message);
Expand Down
20 changes: 20 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,11 @@ public function testValidateGtPlaceHolderIsReplacedProperly()
$this->assertFalse($v->passes());
$this->assertEquals(5, $v->messages()->first('items'));

$v = new Validator($trans, ['max' => 10], ['min' => 'numeric', 'max' => 'numeric|gt:min'], [], ['min' => 'minimum value', 'max' => 'maximum value']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('minimum value', $v->messages()->first('max'));

$file = $this->getMockBuilder(UploadedFile::class)->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
$file->expects($this->any())->method('getSize')->will($this->returnValue(4072));
$file->expects($this->any())->method('isValid')->will($this->returnValue(true));
Expand Down Expand Up @@ -1706,6 +1711,11 @@ public function testValidateLtPlaceHolderIsReplacedProperly()
$this->assertFalse($v->passes());
$this->assertEquals(2, $v->messages()->first('items'));

$v = new Validator($trans, ['min' => 1], ['min' => 'numeric|lt:max', 'max' => 'numeric'], [], ['min' => 'minimum value', 'max' => 'maximum value']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('maximum value', $v->messages()->first('min'));

$file = $this->getMockBuilder(UploadedFile::class)->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
$file->expects($this->any())->method('getSize')->will($this->returnValue(4072));
$file->expects($this->any())->method('isValid')->will($this->returnValue(true));
Expand Down Expand Up @@ -1743,6 +1753,11 @@ public function testValidateGtePlaceHolderIsReplacedProperly()
$this->assertFalse($v->passes());
$this->assertEquals(5, $v->messages()->first('items'));

$v = new Validator($trans, ['max' => 10], ['min' => 'numeric', 'max' => 'numeric|gte:min'], [], ['min' => 'minimum value', 'max' => 'maximum value']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('minimum value', $v->messages()->first('max'));

$file = $this->getMockBuilder(UploadedFile::class)->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
$file->expects($this->any())->method('getSize')->will($this->returnValue(4072));
$file->expects($this->any())->method('isValid')->will($this->returnValue(true));
Expand Down Expand Up @@ -1780,6 +1795,11 @@ public function testValidateLtePlaceHolderIsReplacedProperly()
$this->assertFalse($v->passes());
$this->assertEquals(2, $v->messages()->first('items'));

$v = new Validator($trans, ['min' => 1], ['min' => 'numeric|lte:max', 'max' => 'numeric'], [], ['min' => 'minimum value', 'max' => 'maximum value']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('maximum value', $v->messages()->first('min'));

$file = $this->getMockBuilder(UploadedFile::class)->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
$file->expects($this->any())->method('getSize')->will($this->returnValue(4072));
$file->expects($this->any())->method('isValid')->will($this->returnValue(true));
Expand Down

0 comments on commit 7725e7a

Please sign in to comment.