Skip to content
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

[9.x] New validation rules float and float_between #42522

Closed
wants to merge 2 commits into from

Conversation

taai
Copy link
Contributor

@taai taai commented May 25, 2022

Validation rules digits and digits_between validated, well, digits. They worked perfectly fine since August 6, 2016, for more than 5 years… On January 5, 2022, a single person suddenly woke up and demanded that the rules should actually accept floats (to allow a dot between digits) and his wish was fulfilled the next day.

Since nobody cares that floats are not actually digits and does not want to compromise, here are 2 "new" validation rules that validate floats (digits, but nobody cares about the difference, right?).

Currently, there are no other validation rules (or rule combinations) that can guarantee that the input contains only digits and that the input contains only a specific number of digits.

For example, with the "new" validation rules float and float_between you can validate:

  • PIN codes (float:4 passes 0123 and float_between:4,8 passes 01234567)
  • US Zip codes (float:5 passes 02421)
  • Phone numbers
  • Digits!

@driesvints
Copy link
Member

What about number and number_between?

@taai
Copy link
Contributor Author

taai commented May 26, 2022

What about number and number_between?

I would be very happy to swap the names of float and float_between with digits and digits_between.

Let's talk about floats (really, floats)!

Name float_between or number_between suggests that it does the same as min and max (compares the numeric value, not the number of characters) - that's why I suggest not to use names number, number_between, float, float_between.

The word number is ambiguous - there are many types of numbers (digits, integers, floats, hexadecimals, binary, scientific). If there is a need for validating any type of numbers, use the already existing rule numeric!
Float is unambiguous - every programmer must know what float is and that it may contain a dot (.). In my opinion, decimal could be even better name than float - that makes more sense, because floats are prone to rounding errors, but we are talking about strings and precise length.

Floats/decimals are more complicated than digits. If there would be a rule float (or decimal), it should validate that the input is a valid decimal number (that is either zero or does not star with zero like 01 or 00.123) at all. And I would not be interested in the complete length of the whole input, but I would be interested in precision - how many digits are before the dot and how many digits are after the dot. For example, when I'm validating product price, I'm talking about numbers like 1.99 (I suggest decimal:12,2), but when I'm validating GPS coordinates (using the same precision what Google Maps use), I'm talking about numbers like 60.8656662 (I suggest decimal:9,7) and 159.6511265 (I suggest decimal:10,7). And probably there is no need for decimal_between rule for floats/decimals when we are talking about precision - usually the minimum number of digits does not matter, only the maximum. And let's not forget that floats can be also negative numbers!

And if we are talking about decimal_between rule - it should not compare precision of digits, but it should compare the float/decimal value itself by using PHP function bccomp. For example, if I would want the user pay at least one cent, I would use validation rule decimal_between:0.01,1000000,2 (the third parameter would check the precision - limit the number of digits after the dot).

In conclusion...

  • Do you agree to change back the previous behavior of rules digits and digits_between?
  • Shall I make a validation rule decimal and decimal_between as I described them?

I'm willing to program all of this, of course, just give me a green light! 😉

@taylorotwell
Copy link
Member

Honestly I wonder if that dot change should just be reverted entirely.

@taylorotwell
Copy link
Member

taylorotwell commented May 26, 2022

Do you actually need a new validation rule if you just combine the integer and digits rules?

'pin' => 'required|integer|digits:4'

@taai
Copy link
Contributor Author

taai commented May 26, 2022

Do you actually need a new validation rule if you just combine the integer and digits rules?

'pin' => 'required|integer|digits:4'

@taylorotwell As I have already answered you, the validation rule integer will fail on pin number 0123.

$value = '0123';
$isValid = filter_var($value, FILTER_VALIDATE_INT) !== false; // false

Honestly I wonder if that dot change should just be reverted entirely.

Yes, please!

@taylorotwell
Copy link
Member

Yeah, agree. Reverting it now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants