Skip to content

Commit c45e1ed

Browse files
authored
Fix the bug that not support compare numeric in different formats (#5808)
* fix(validate): support compare numeric in different type * chore(style): format code * fix(validate): support compare numeric in different type * chore(style): format code * chore(docs): add changes to changelog
1 parent a582a01 commit c45e1ed

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Concerns/ValidatesAttributes.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,10 @@ public function validateGt(string $attribute, $value, array $parameters): bool
533533
return $this->getSize($attribute, $value) > $parameters[0];
534534
}
535535

536+
if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
537+
return $value > $comparedToValue;
538+
}
539+
536540
if (! $this->isSameType($value, $comparedToValue)) {
537541
return false;
538542
}
@@ -557,6 +561,10 @@ public function validateLt(string $attribute, $value, array $parameters): bool
557561
return $this->getSize($attribute, $value) < $parameters[0];
558562
}
559563

564+
if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
565+
return $value < $comparedToValue;
566+
}
567+
560568
if (! $this->isSameType($value, $comparedToValue)) {
561569
return false;
562570
}
@@ -581,6 +589,10 @@ public function validateGte(string $attribute, $value, array $parameters): bool
581589
return $this->getSize($attribute, $value) >= $parameters[0];
582590
}
583591

592+
if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
593+
return $value >= $comparedToValue;
594+
}
595+
584596
if (! $this->isSameType($value, $comparedToValue)) {
585597
return false;
586598
}
@@ -605,6 +617,10 @@ public function validateLte(string $attribute, $value, array $parameters): bool
605617
return $this->getSize($attribute, $value) <= $parameters[0];
606618
}
607619

620+
if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
621+
return $value <= $comparedToValue;
622+
}
623+
608624
if (! $this->isSameType($value, $comparedToValue)) {
609625
return false;
610626
}

tests/Cases/ValidationValidatorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,9 @@ public function testValidateGtPlaceHolderIsReplacedProperly()
16921692
$this->assertFalse($v->passes());
16931693
$this->assertEquals(5, $v->messages()->first('items'));
16941694

1695+
$v = new Validator($trans, ['items' => '26', 'more' => 5], ['items' => 'gt:more']);
1696+
$this->assertTrue($v->passes());
1697+
16951698
$v = new Validator($trans, ['items' => 'abc', 'more' => 'abcde'], ['items' => 'gt:more']);
16961699
$this->assertFalse($v->passes());
16971700
$this->assertEquals(5, $v->messages()->first('items'));
@@ -1729,6 +1732,9 @@ public function testValidateLtPlaceHolderIsReplacedProperly()
17291732
$this->assertFalse($v->passes());
17301733
$this->assertEquals(2, $v->messages()->first('items'));
17311734

1735+
$v = new Validator($trans, ['items' => '5', 'less' => 26], ['items' => 'numeric|lt:less']);
1736+
$this->assertTrue($v->passes());
1737+
17321738
$v = new Validator($trans, ['items' => 'abc', 'less' => 'ab'], ['items' => 'lt:less']);
17331739
$this->assertFalse($v->passes());
17341740
$this->assertEquals(2, $v->messages()->first('items'));
@@ -1766,6 +1772,9 @@ public function testValidateGtePlaceHolderIsReplacedProperly()
17661772
$this->assertFalse($v->passes());
17671773
$this->assertEquals(5, $v->messages()->first('items'));
17681774

1775+
$v = new Validator($trans, ['items' => '26', 'more' => 26], ['items' => 'numeric|gte:more']);
1776+
$this->assertTrue($v->passes());
1777+
17691778
$v = new Validator($trans, ['items' => 'abc', 'more' => 'abcde'], ['items' => 'gte:more']);
17701779
$this->assertFalse($v->passes());
17711780
$this->assertEquals(5, $v->messages()->first('items'));
@@ -1803,6 +1812,9 @@ public function testValidateLtePlaceHolderIsReplacedProperly()
18031812
$this->assertFalse($v->passes());
18041813
$this->assertEquals(2, $v->messages()->first('items'));
18051814

1815+
$v = new Validator($trans, ['items' => 5, 'less' => '26'], ['items' => 'numeric|lte:less']);
1816+
$this->assertTrue($v->passes());
1817+
18061818
$v = new Validator($trans, ['items' => 'abc', 'less' => 'ab'], ['items' => 'lte:less']);
18071819
$this->assertFalse($v->passes());
18081820
$this->assertEquals(2, $v->messages()->first('items'));

0 commit comments

Comments
 (0)