From e9f740e5f4b41e4273b0bf28b53b1707776a69f1 Mon Sep 17 00:00:00 2001 From: Aly Suleiman Date: Mon, 7 May 2018 16:43:20 +0200 Subject: [PATCH] Remove automatic detection of numeric attributes and implement replacement placeholders for the rules (#24135) --- .../Concerns/ReplacesAttributes.php | 56 +++++++++++++++++++ src/Illuminate/Validation/Validator.php | 2 +- tests/Validation/ValidationValidatorTest.php | 16 +++--- 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php index 76a2a98a4024..6ff93dce9ee6 100644 --- a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php @@ -76,6 +76,62 @@ protected function replaceDigitsBetween($message, $attribute, $rule, $parameters return $this->replaceBetween($message, $attribute, $rule, $parameters); } + /** + * Replace all place-holders for the gt rule. + * + * @param string $message + * @param string $attribute + * @param string $rule + * @param array $parameters + * @return string + */ + protected function replaceGt($message, $attribute, $rule, $parameters) + { + return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message); + } + + /** + * Replace all place-holders for the lt rule. + * + * @param string $message + * @param string $attribute + * @param string $rule + * @param array $parameters + * @return string + */ + protected function replaceLt($message, $attribute, $rule, $parameters) + { + return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message); + } + + /** + * Replace all place-holders for the gte rule. + * + * @param string $message + * @param string $attribute + * @param string $rule + * @param array $parameters + * @return string + */ + protected function replaceGte($message, $attribute, $rule, $parameters) + { + return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message); + } + + /** + * Replace all place-holders for the lte rule. + * + * @param string $message + * @param string $attribute + * @param string $rule + * @param array $parameters + * @return string + */ + protected function replaceLte($message, $attribute, $rule, $parameters) + { + return str_replace(':value', $this->getSize($parameters[0], $this->getValue($parameters[0])), $message); + } + /** * Replace all place-holders for the min rule. * diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index a938c7e10df8..7d0ebb1d5d69 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -182,7 +182,7 @@ class Validator implements ValidatorContract * * @var array */ - protected $numericRules = ['Numeric', 'Integer', 'Gt', 'Lt', 'Gte', 'Lte']; + protected $numericRules = ['Numeric', 'Integer']; /** * Create a new Validator instance. diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 81d30deb7b58..6ec714e4a814 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -1063,7 +1063,7 @@ public function testValidateDifferent() public function testGreaterThan() { $trans = $this->getIlluminateArrayTranslator(); - $v = new Validator($trans, ['lhs' => 15, 'rhs' => 10], ['lhs' => 'gt:rhs']); + $v = new Validator($trans, ['lhs' => 15, 'rhs' => 10], ['lhs' => 'numeric|gt:rhs']); $this->assertTrue($v->passes()); $v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'gt:rhs']); @@ -1079,14 +1079,14 @@ public function testGreaterThan() $v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'gt:rhs']); $this->assertTrue($v->passes()); - $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'gt:10']); + $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|gt:10']); $this->assertTrue($v->passes()); } public function testLessThan() { $trans = $this->getIlluminateArrayTranslator(); - $v = new Validator($trans, ['lhs' => 15, 'rhs' => 10], ['lhs' => 'lt:rhs']); + $v = new Validator($trans, ['lhs' => 15, 'rhs' => 10], ['lhs' => 'numeric|lt:rhs']); $this->assertTrue($v->fails()); $v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'lt:rhs']); @@ -1102,14 +1102,14 @@ public function testLessThan() $v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'lt:rhs']); $this->assertTrue($v->fails()); - $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'lt:10']); + $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|lt:10']); $this->assertTrue($v->fails()); } public function testGreaterThanOrEqual() { $trans = $this->getIlluminateArrayTranslator(); - $v = new Validator($trans, ['lhs' => 15, 'rhs' => 15], ['lhs' => 'gte:rhs']); + $v = new Validator($trans, ['lhs' => 15, 'rhs' => 15], ['lhs' => 'numeric|gte:rhs']); $this->assertTrue($v->passes()); $v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'gte:rhs']); @@ -1125,14 +1125,14 @@ public function testGreaterThanOrEqual() $v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'gte:rhs']); $this->assertTrue($v->passes()); - $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'gte:15']); + $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|gte:15']); $this->assertTrue($v->passes()); } public function testLessThanOrEqual() { $trans = $this->getIlluminateArrayTranslator(); - $v = new Validator($trans, ['lhs' => 15, 'rhs' => 15], ['lhs' => 'lte:rhs']); + $v = new Validator($trans, ['lhs' => 15, 'rhs' => 15], ['lhs' => 'numeric|lte:rhs']); $this->assertTrue($v->passes()); $v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'lte:rhs']); @@ -1148,7 +1148,7 @@ public function testLessThanOrEqual() $v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'lte:rhs']); $this->assertTrue($v->passes()); - $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'lte:10']); + $v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|lte:10']); $this->assertTrue($v->fails()); }