Skip to content

Commit

Permalink
[8.x] Make multiple_of validation rule handle non-integer values (#…
Browse files Browse the repository at this point in the history
…34971)

* Fix #34960

* Add multiple_of tests where fmod fails

* Update ValidatesAttributes.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
tontonsb and taylorotwell authored Oct 26, 2020
1 parent c393ca2 commit c5ab5f7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,11 @@ public function validateMultipleOf($attribute, $value, $parameters)
return false;
}

return fmod($value, $parameters[0]) === 0.0;
if ((float) $parameters[0] === 0.0) {
return false;
}

return bcmod($value, $parameters[0], 16) === '0.0000000000000000';
}

/**
Expand Down
9 changes: 6 additions & 3 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1912,18 +1912,21 @@ public function multipleOfDataProvider()
[5.0, -10, false],
[10.5, 10.5, true], // float (same)
[10.5, 0.5, true], // float + float
[10.5, 0.3, false],
[10.5, 0.3, true], // 10.5/.3 = 35, tricky for floating point division
[31.5, 10.5, true],
[31.6, 10.5, false],
[10.5, -0.5, true], // float + -float
[10.5, -0.3, false],
[10.5, -0.3, true], // 10.5/.3 = 35, tricky for floating point division
[-31.5, 10.5, true],
[-31.6, 10.5, false],
[-10.5, -10.5, true], // -float (same)
[-10.5, -0.5, true], // -float + -float
[-10.5, -0.3, false],
[-10.5, -0.3, true], // 10.5/.3 = 35, tricky for floating point division
[-31.5, -10.5, true],
[-31.6, -10.5, false],
[2, .1, true], // fmod does this "wrong", it should be 0, but fmod(2, .1) = .1
[.75, .05, true], // fmod does this "wrong", it should be 0, but fmod(.75, .05) = .05
[.9, .3, true], // .9/.3 = 3, tricky for floating point division
['foo', 1, false], // invalid values
[1, 'foo', false],
['foo', 'foo', false],
Expand Down

0 comments on commit c5ab5f7

Please sign in to comment.