diff --git a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php index 6ebdfddd7f20..3594dd9b3540 100644 --- a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php @@ -259,7 +259,11 @@ protected function replaceSize($message, $attribute, $rule, $parameters) */ protected function replaceGt($message, $attribute, $rule, $parameters) { - return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message); + if (is_null($value = $this->getValue($parameters[0]))) { + return str_replace(':value', $parameters[0], $message); + } + + return str_replace(':value', $this->getSize($attribute, $value), $message); } /** @@ -273,7 +277,11 @@ protected function replaceGt($message, $attribute, $rule, $parameters) */ protected function replaceLt($message, $attribute, $rule, $parameters) { - return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message); + if (is_null($value = $this->getValue($parameters[0]))) { + return str_replace(':value', $parameters[0], $message); + } + + return str_replace(':value', $this->getSize($attribute, $value), $message); } /** @@ -287,7 +295,11 @@ protected function replaceLt($message, $attribute, $rule, $parameters) */ protected function replaceGte($message, $attribute, $rule, $parameters) { - return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message); + if (is_null($value = $this->getValue($parameters[0]))) { + return str_replace(':value', $parameters[0], $message); + } + + return str_replace(':value', $this->getSize($attribute, $value), $message); } /** @@ -301,7 +313,11 @@ protected function replaceGte($message, $attribute, $rule, $parameters) */ protected function replaceLte($message, $attribute, $rule, $parameters) { - return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message); + if (is_null($value = $this->getValue($parameters[0]))) { + return str_replace(':value', $parameters[0], $message); + } + + return str_replace(':value', $this->getSize($attribute, $value), $message); } /** diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 98b7fee0c04d..5fa2cda1bb71 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -1533,6 +1533,154 @@ public function testProperMessagesAreReturnedForSizes() $this->assertEquals('file', $v->messages()->first('photo')); } + public function testValidateGtPlaceHolderIsReplacedProperly() + { + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines([ + 'validation.gt.numeric' => ':value', + 'validation.gt.string' => ':value', + 'validation.gt.file' => ':value', + 'validation.gt.array' => ':value', + ], 'en'); + + $v = new Validator($trans, ['items' => '3'], ['items' => 'gt:4']); + $this->assertFalse($v->passes()); + $this->assertEquals(4, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 3, 'more' => 5], ['items' => 'numeric|gt:more']); + $this->assertFalse($v->passes()); + $this->assertEquals(5, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 'abc', 'more' => 'abcde'], ['items' => 'gt:more']); + $this->assertFalse($v->passes()); + $this->assertEquals(5, $v->messages()->first('items')); + + $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->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)); + $biggerFile = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock(); + $biggerFile->expects($this->any())->method('getSize')->will($this->returnValue(5120)); + $biggerFile->expects($this->any())->method('isValid')->will($this->returnValue(true)); + $v = new Validator($trans, ['photo' => $file, 'bigger' => $biggerFile], ['photo' => 'file|gt:bigger']); + $this->assertFalse($v->passes()); + $this->assertEquals(5, $v->messages()->first('photo')); + + $v = new Validator($trans, ['items' => [1, 2, 3], 'more' => [0, 1, 2, 3]], ['items' => 'gt:more']); + $this->assertFalse($v->passes()); + $this->assertEquals(4, $v->messages()->first('items')); + } + + public function testValidateLtPlaceHolderIsReplacedProperly() + { + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines([ + 'validation.lt.numeric' => ':value', + 'validation.lt.string' => ':value', + 'validation.lt.file' => ':value', + 'validation.lt.array' => ':value', + ], 'en'); + + $v = new Validator($trans, ['items' => '3'], ['items' => 'lt:2']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 3, 'less' => 2], ['items' => 'numeric|lt:less']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 'abc', 'less' => 'ab'], ['items' => 'lt:less']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + + $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->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)); + $smallerFile = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock(); + $smallerFile->expects($this->any())->method('getSize')->will($this->returnValue(2048)); + $smallerFile->expects($this->any())->method('isValid')->will($this->returnValue(true)); + $v = new Validator($trans, ['photo' => $file, 'smaller' => $smallerFile], ['photo' => 'file|lt:smaller']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('photo')); + + $v = new Validator($trans, ['items' => [1, 2, 3], 'less' => [0, 1]], ['items' => 'lt:less']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + } + + public function testValidateGtePlaceHolderIsReplacedProperly() + { + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines([ + 'validation.gte.numeric' => ':value', + 'validation.gte.string' => ':value', + 'validation.gte.file' => ':value', + 'validation.gte.array' => ':value', + ], 'en'); + + $v = new Validator($trans, ['items' => '3'], ['items' => 'gte:4']); + $this->assertFalse($v->passes()); + $this->assertEquals(4, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 3, 'more' => 5], ['items' => 'numeric|gte:more']); + $this->assertFalse($v->passes()); + $this->assertEquals(5, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 'abc', 'more' => 'abcde'], ['items' => 'gte:more']); + $this->assertFalse($v->passes()); + $this->assertEquals(5, $v->messages()->first('items')); + + $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->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)); + $biggerFile = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock(); + $biggerFile->expects($this->any())->method('getSize')->will($this->returnValue(5120)); + $biggerFile->expects($this->any())->method('isValid')->will($this->returnValue(true)); + $v = new Validator($trans, ['photo' => $file, 'bigger' => $biggerFile], ['photo' => 'file|gte:bigger']); + $this->assertFalse($v->passes()); + $this->assertEquals(5, $v->messages()->first('photo')); + + $v = new Validator($trans, ['items' => [1, 2, 3], 'more' => [0, 1, 2, 3]], ['items' => 'gte:more']); + $this->assertFalse($v->passes()); + $this->assertEquals(4, $v->messages()->first('items')); + } + + public function testValidateLtePlaceHolderIsReplacedProperly() + { + $trans = $this->getIlluminateArrayTranslator(); + $trans->addLines([ + 'validation.lte.numeric' => ':value', + 'validation.lte.string' => ':value', + 'validation.lte.file' => ':value', + 'validation.lte.array' => ':value', + ], 'en'); + + $v = new Validator($trans, ['items' => '3'], ['items' => 'lte:2']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 3, 'less' => 2], ['items' => 'numeric|lte:less']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + + $v = new Validator($trans, ['items' => 'abc', 'less' => 'ab'], ['items' => 'lte:less']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + + $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->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)); + $smallerFile = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock(); + $smallerFile->expects($this->any())->method('getSize')->will($this->returnValue(2048)); + $smallerFile->expects($this->any())->method('isValid')->will($this->returnValue(true)); + $v = new Validator($trans, ['photo' => $file, 'smaller' => $smallerFile], ['photo' => 'file|lte:smaller']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('photo')); + + $v = new Validator($trans, ['items' => [1, 2, 3], 'less' => [0, 1]], ['items' => 'lte:less']); + $this->assertFalse($v->passes()); + $this->assertEquals(2, $v->messages()->first('items')); + } + public function testValidateIn() { $trans = $this->getIlluminateArrayTranslator();