Skip to content

Commit d8a0931

Browse files
apihtaylorotwell
andauthored
[10.x] Reset numeric rules after each attribute's validation (#49142)
* Reset numeric rules after each attribute's validation * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 9bc5b59 commit d8a0931

File tree

2 files changed

+183
-8
lines changed

2 files changed

+183
-8
lines changed

src/Illuminate/Validation/Validator.php

+9
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ class Validator implements ValidatorContract
290290
*/
291291
protected $numericRules = ['Numeric', 'Integer', 'Decimal'];
292292

293+
/**
294+
* The default numeric related validation rules.
295+
*
296+
* @var string[]
297+
*/
298+
protected $defaultNumericRules = ['Numeric', 'Integer', 'Decimal'];
299+
293300
/**
294301
* The current placeholder for dots in rule keys.
295302
*
@@ -641,6 +648,8 @@ protected function validateAttribute($attribute, $rule)
641648

642649
$method = "validate{$rule}";
643650

651+
$this->numericRules = $this->defaultNumericRules;
652+
644653
if ($validatable && ! $this->$method($attribute, $value, $parameters, $this)) {
645654
$this->addFailure($attribute, $rule, $parameters);
646655
}

tests/Validation/ValidationValidatorTest.php

+174-8
Original file line numberDiff line numberDiff line change
@@ -3955,6 +3955,166 @@ public function testValidationExistsIsNotCalledUnnecessarily()
39553955
$this->assertTrue($v->passes());
39563956
}
39573957

3958+
public function testValidateGtMessagesAreCorrect()
3959+
{
3960+
$trans = $this->getIlluminateArrayTranslator();
3961+
$trans->addLines([
3962+
'validation.gt.numeric' => 'The :attribute field must be greater than :value.',
3963+
'validation.gt.string' => 'The :attribute field must be greater than :value characters.',
3964+
'validation.gt.file' => 'The :attribute field must be greater than :value kilobytes.',
3965+
'validation.gt.array' => 'The :attribute field must have more than :value items.',
3966+
], 'en');
3967+
3968+
$file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
3969+
$file->expects($this->any())->method('getSize')->willReturn(8919);
3970+
$file->expects($this->any())->method('isValid')->willReturn(true);
3971+
$otherFile = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
3972+
$otherFile->expects($this->any())->method('getSize')->willReturn(9216);
3973+
$otherFile->expects($this->any())->method('isValid')->willReturn(true);
3974+
3975+
$v = new Validator($trans, [
3976+
'numeric' => 7,
3977+
'string' => 'abcd',
3978+
'file' => $file,
3979+
'array' => [1, 2, 3],
3980+
'other_numeric' => 10,
3981+
'other_string' => 'abcde',
3982+
'other_file' => $otherFile,
3983+
'other_array' => [1, 2, 3, 4],
3984+
], [
3985+
'numeric' => 'gt:other_numeric',
3986+
'string' => 'gt:other_string',
3987+
'file' => 'gt:other_file',
3988+
'array' => 'array|gt:other_array',
3989+
]);
3990+
3991+
$this->assertFalse($v->passes());
3992+
$this->assertEquals('The numeric field must be greater than 10.', $v->messages()->first('numeric'));
3993+
$this->assertEquals('The string field must be greater than 5 characters.', $v->messages()->first('string'));
3994+
$this->assertEquals('The file field must be greater than 9 kilobytes.', $v->messages()->first('file'));
3995+
$this->assertEquals('The array field must have more than 4 items.', $v->messages()->first('array'));
3996+
}
3997+
3998+
public function testValidateGteMessagesAreCorrect()
3999+
{
4000+
$trans = $this->getIlluminateArrayTranslator();
4001+
$trans->addLines([
4002+
'validation.gte.numeric' => 'The :attribute field must be greater than or equal to :value.',
4003+
'validation.gte.string' => 'The :attribute field must be greater than or equal to :value characters.',
4004+
'validation.gte.file' => 'The :attribute field must be greater than or equal to :value kilobytes.',
4005+
'validation.gte.array' => 'The :attribute field must have :value items or more.',
4006+
], 'en');
4007+
4008+
$file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
4009+
$file->expects($this->any())->method('getSize')->willReturn(8919);
4010+
$file->expects($this->any())->method('isValid')->willReturn(true);
4011+
$otherFile = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
4012+
$otherFile->expects($this->any())->method('getSize')->willReturn(9216);
4013+
$otherFile->expects($this->any())->method('isValid')->willReturn(true);
4014+
4015+
$v = new Validator($trans, [
4016+
'numeric' => 7,
4017+
'string' => 'abcd',
4018+
'file' => $file,
4019+
'array' => [1, 2, 3],
4020+
'other_numeric' => 10,
4021+
'other_string' => 'abcde',
4022+
'other_file' => $otherFile,
4023+
'other_array' => [1, 2, 3, 4],
4024+
], [
4025+
'numeric' => 'gte:other_numeric',
4026+
'string' => 'gte:other_string',
4027+
'file' => 'gte:other_file',
4028+
'array' => 'array|gte:other_array',
4029+
]);
4030+
4031+
$this->assertFalse($v->passes());
4032+
$this->assertEquals('The numeric field must be greater than or equal to 10.', $v->messages()->first('numeric'));
4033+
$this->assertEquals('The string field must be greater than or equal to 5 characters.', $v->messages()->first('string'));
4034+
$this->assertEquals('The file field must be greater than or equal to 9 kilobytes.', $v->messages()->first('file'));
4035+
$this->assertEquals('The array field must have 4 items or more.', $v->messages()->first('array'));
4036+
}
4037+
4038+
public function testValidateLtMessagesAreCorrect()
4039+
{
4040+
$trans = $this->getIlluminateArrayTranslator();
4041+
$trans->addLines([
4042+
'validation.lt.numeric' => 'The :attribute field must be less than :value.',
4043+
'validation.lt.string' => 'The :attribute field must be less than :value characters.',
4044+
'validation.lt.file' => 'The :attribute field must be less than :value kilobytes.',
4045+
'validation.lt.array' => 'The :attribute field must have less than :value items.',
4046+
], 'en');
4047+
4048+
$file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
4049+
$file->expects($this->any())->method('getSize')->willReturn(8919);
4050+
$file->expects($this->any())->method('isValid')->willReturn(true);
4051+
$otherFile = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
4052+
$otherFile->expects($this->any())->method('getSize')->willReturn(8192);
4053+
$otherFile->expects($this->any())->method('isValid')->willReturn(true);
4054+
4055+
$v = new Validator($trans, [
4056+
'numeric' => 7,
4057+
'string' => 'abcd',
4058+
'file' => $file,
4059+
'array' => [1, 2, 3],
4060+
'other_numeric' => 5,
4061+
'other_string' => 'abc',
4062+
'other_file' => $otherFile,
4063+
'other_array' => [1, 2],
4064+
], [
4065+
'numeric' => 'lt:other_numeric',
4066+
'string' => 'lt:other_string',
4067+
'file' => 'lt:other_file',
4068+
'array' => 'array|lt:other_array',
4069+
]);
4070+
4071+
$this->assertFalse($v->passes());
4072+
$this->assertEquals('The numeric field must be less than 5.', $v->messages()->first('numeric'));
4073+
$this->assertEquals('The string field must be less than 3 characters.', $v->messages()->first('string'));
4074+
$this->assertEquals('The file field must be less than 8 kilobytes.', $v->messages()->first('file'));
4075+
$this->assertEquals('The array field must have less than 2 items.', $v->messages()->first('array'));
4076+
}
4077+
4078+
public function testValidateLteMessagesAreCorrect()
4079+
{
4080+
$trans = $this->getIlluminateArrayTranslator();
4081+
$trans->addLines([
4082+
'validation.lte.numeric' => 'The :attribute field must be less than or equal to :value.',
4083+
'validation.lte.string' => 'The :attribute field must be less than or equal to :value characters.',
4084+
'validation.lte.file' => 'The :attribute field must be less than or equal to :value kilobytes.',
4085+
'validation.lte.array' => 'The :attribute field must not have more than :value items.',
4086+
], 'en');
4087+
4088+
$file = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
4089+
$file->expects($this->any())->method('getSize')->willReturn(8919);
4090+
$file->expects($this->any())->method('isValid')->willReturn(true);
4091+
$otherFile = $this->getMockBuilder(UploadedFile::class)->onlyMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock();
4092+
$otherFile->expects($this->any())->method('getSize')->willReturn(8192);
4093+
$otherFile->expects($this->any())->method('isValid')->willReturn(true);
4094+
4095+
$v = new Validator($trans, [
4096+
'numeric' => 7,
4097+
'string' => 'abcd',
4098+
'file' => $file,
4099+
'array' => [1, 2, 3],
4100+
'other_numeric' => 5,
4101+
'other_string' => 'abc',
4102+
'other_file' => $otherFile,
4103+
'other_array' => [1, 2],
4104+
], [
4105+
'numeric' => 'lte:other_numeric',
4106+
'string' => 'lte:other_string',
4107+
'file' => 'lte:other_file',
4108+
'array' => 'array|lte:other_array',
4109+
]);
4110+
4111+
$this->assertFalse($v->passes());
4112+
$this->assertEquals('The numeric field must be less than or equal to 5.', $v->messages()->first('numeric'));
4113+
$this->assertEquals('The string field must be less than or equal to 3 characters.', $v->messages()->first('string'));
4114+
$this->assertEquals('The file field must be less than or equal to 8 kilobytes.', $v->messages()->first('file'));
4115+
$this->assertEquals('The array field must not have more than 2 items.', $v->messages()->first('array'));
4116+
}
4117+
39584118
public function testValidateIp()
39594119
{
39604120
$trans = $this->getIlluminateArrayTranslator();
@@ -8917,6 +9077,9 @@ public function testItTrimsSpaceFromParameters()
89179077
'foo' => '4',
89189078
' foo' => ' 5',
89199079
' foo ' => ' 6 ',
9080+
'foo_str' => 'abcd',
9081+
' foo_str' => ' abcd',
9082+
' foo_str ' => ' abcd ',
89209083
], [
89219084
'min' => 'numeric|min: 20',
89229085
'min_str' => 'min: 5',
@@ -8925,16 +9088,16 @@ public function testItTrimsSpaceFromParameters()
89259088
'between_str' => "between:\t 5, 6\n",
89269089
'gt' => 'numeric|gt: 4',
89279090
'gt_field' => 'numeric|gt:foo',
8928-
'gt_str' => 'gt:foo',
9091+
'gt_str' => 'gt:foo_str',
89299092
'lt' => 'numeric|lt: 6',
89309093
'lt_field' => 'numeric|lt: foo ',
8931-
'lt_str' => 'lt: foo ',
9094+
'lt_str' => 'lt: foo_str ',
89329095
'gte' => 'numeric|gte: 5',
89339096
'gte_field' => 'numeric|gte: foo',
8934-
'gte_str' => 'gte: foo',
9097+
'gte_str' => 'gte: foo_str',
89359098
'lte' => 'numeric|lte: 5',
89369099
'lte_field' => 'numeric|lte: foo',
8937-
'lte_str' => 'lte: foo',
9100+
'lte_str' => 'lte: foo_str',
89389101
'max' => 'numeric|max: 20',
89399102
'max_str' => 'max: 5',
89409103
'size' => 'numeric|size: 20',
@@ -8967,6 +9130,9 @@ public function testItTrimsSpaceFromParameters()
89679130
'foo' => '4',
89689131
' foo' => ' 5',
89699132
' foo ' => ' 6 ',
9133+
'foo_str' => 'abcd',
9134+
' foo_str' => ' abcd',
9135+
' foo_str ' => ' abcd ',
89709136
], [
89719137
'min' => 'numeric|min: 21',
89729138
'min_str' => 'min: 6',
@@ -8975,16 +9141,16 @@ public function testItTrimsSpaceFromParameters()
89759141
'between_str' => "between:\t 6, 7\n",
89769142
'gt' => 'numeric|gt: 5',
89779143
'gt_field' => 'numeric|gt: foo ',
8978-
'gt_str' => 'gt: foo',
9144+
'gt_str' => 'gt: foo_str',
89799145
'lt' => 'numeric|lt: 5',
89809146
'lt_field' => 'numeric|lt: foo',
8981-
'lt_str' => 'lt: foo',
9147+
'lt_str' => 'lt: foo_str',
89829148
'gte' => 'numeric|gte: 6',
89839149
'gte_field' => 'numeric|gte: foo ',
8984-
'gte_str' => 'gte: foo ',
9150+
'gte_str' => 'gte: foo_str ',
89859151
'lte' => 'numeric|lte: 4',
89869152
'lte_field' => 'numeric|lte:foo',
8987-
'lte_str' => 'lte:foo',
9153+
'lte_str' => 'lte:foo_str',
89889154
'max' => 'numeric|max: 19',
89899155
'max_str' => 'max: 4',
89909156
'size' => 'numeric|size: 19',

0 commit comments

Comments
 (0)