Skip to content

[8.x] Make multiple_of validation rule handle non-integer values #34971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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