Skip to content

Validation rule multiple_of fails on many cases with decimal step values #34960

Closed
@tontonsb

Description

  • Laravel Version: 8.11.2
  • PHP Version: 7.4.3

Description:

The multiple_of validation rule fails in all those cases where PHP's fmod works in an unexpected way.

The initial PR #34788 intended to make this a counterpart of HTML's step:

This validation rule can be useful if you use a number input with a step attribute.

But as it currently works it does not mirror the behaviour of HTML's step on contemporary browsers, for example

<input type=number min=0 step=0.3 >

allows me to enter 0.9, but it fails the multiple_of:0.3 validation.

Steps To Reproduce:

See #34959 for failing tests.

Solutions?

I could attempt to implement a solution, but I am not sure what Laravel wants to have here. There's a lot of possible directions

  • Leave it as is and claim to only support integer steps
  • Implement a direct and more correct computation instead of using fmod
  • Add a more proper fmod replacement to helpers
  • Use a third party library that does this correctly (I don't think there's anything native in PHP that solves this) like https://github.com/simplito/bn-php

And do we want to support the HTML's behaviour that step is evaluated relative to min? I.e. if min=10 step=3 HTML would allow 10, 13, 16, ... while Laravel's validator with 'min:10', 'multiple_of:3' would allow 12, 15, ...

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions