Skip to content

Commit 6007713

Browse files
authored
Merge pull request #7098 from kenjis/fix-validation-strict-greater_than
fix: handling float in Validation Strcit Rules (greater_than, greater_than_equal_to, less_than, less_than_equal_to)
2 parents 3dbad8c + 65d9b21 commit 6007713

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

system/Validation/StrictRules/Rules.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function exact_length($str, string $val): bool
7575
*/
7676
public function greater_than($str, string $min): bool
7777
{
78-
if (is_int($str)) {
78+
if (is_int($str) || is_float($str)) {
7979
$str = (string) $str;
8080
}
8181

@@ -93,7 +93,7 @@ public function greater_than($str, string $min): bool
9393
*/
9494
public function greater_than_equal_to($str, string $min): bool
9595
{
96-
if (is_int($str)) {
96+
if (is_int($str) || is_float($str)) {
9797
$str = (string) $str;
9898
}
9999

@@ -213,7 +213,7 @@ public function is_unique($str, string $field, array $data): bool
213213
*/
214214
public function less_than($str, string $max): bool
215215
{
216-
if (is_int($str)) {
216+
if (is_int($str) || is_float($str)) {
217217
$str = (string) $str;
218218
}
219219

@@ -231,7 +231,7 @@ public function less_than($str, string $max): bool
231231
*/
232232
public function less_than_equal_to($str, string $max): bool
233233
{
234-
if (is_int($str)) {
234+
if (is_int($str) || is_float($str)) {
235235
$str = (string) $str;
236236
}
237237

tests/system/Validation/RulesTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ public function greaterThanProvider(): Generator
427427
['-10', '-11', true],
428428
['10', '9', true],
429429
['10', '10', false],
430+
['10.1', '10', true],
431+
['10.0', '10', false],
432+
['9.9', '10', false],
430433
['10', 'a', false],
431434
['10a', '10', false],
432435
[null, null, false],
@@ -449,6 +452,9 @@ public function greaterThanEqualProvider(): Generator
449452
['0', '0', true],
450453
['1', '0', true],
451454
['-1', '0', false],
455+
['1.0', '1', true],
456+
['1.1', '1', true],
457+
['0.9', '1', false],
452458
['10a', '0', false],
453459
[null, null, false],
454460
['1', null, true],
@@ -473,6 +479,9 @@ public function lessThanProvider(): Generator
473479
['9', '10', true],
474480
['10', '9', false],
475481
['10', '10', false],
482+
['9.9', '10', true],
483+
['10.1', '10', false],
484+
['10.0', '10', false],
476485
['10', 'a', true],
477486
['10a', '10', false],
478487
[null, null, false],
@@ -482,7 +491,7 @@ public function lessThanProvider(): Generator
482491
/**
483492
* @dataProvider lessThanEqualProvider
484493
*/
485-
public function testLessEqualThan(?string $first, ?string $second, bool $expected): void
494+
public function testLessThanEqual(?string $first, ?string $second, bool $expected): void
486495
{
487496
$data = ['foo' => $first];
488497
$this->validation->setRules(['foo' => "less_than_equal_to[{$second}]"]);
@@ -495,6 +504,9 @@ public function lessThanEqualProvider(): Generator
495504
['0', '0', true],
496505
['1', '0', false],
497506
['-1', '0', true],
507+
['1.0', '1', true],
508+
['0.9', '1', true],
509+
['1.1', '1', false],
498510
['10a', '0', false],
499511
[null, null, false],
500512
['1', null, false],

tests/system/Validation/StrictRules/RulesTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ public function provideGreaterThanEqualStrict(): Generator
106106
[0, '0', true],
107107
[1, '0', true],
108108
[-1, '0', false],
109+
[1.0, '1', true],
110+
[1.1, '1', true],
111+
[0.9, '1', false],
109112
[true, '0', false],
110113
];
111114
}
@@ -129,6 +132,9 @@ public function provideGreaterThanStrict(): Generator
129132
[-10, '-11', true],
130133
[10, '9', true],
131134
[10, '10', false],
135+
[10.1, '10', true],
136+
[10.0, '10', false],
137+
[9.9, '10', false],
132138
[10, 'a', false],
133139
[true, '0', false],
134140
];
@@ -154,6 +160,9 @@ public function provideLessThanStrict(): Generator
154160
[9, '10', true],
155161
[10, '9', false],
156162
[10, '10', false],
163+
[9.9, '10', true],
164+
[10.1, '10', false],
165+
[10.0, '10', false],
157166
[10, 'a', true],
158167
[true, '0', false],
159168
];
@@ -178,6 +187,9 @@ public function provideLessThanEqualStrict(): Generator
178187
[0, '0', true],
179188
[1, '0', false],
180189
[-1, '0', true],
190+
[1.0, '1', true],
191+
[0.9, '1', true],
192+
[1.1, '1', false],
181193
[true, '0', false],
182194
];
183195
}

0 commit comments

Comments
 (0)