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] Add decimal validation rule #45356

Merged
merged 2 commits into from Dec 19, 2022
Merged

[9.x] Add decimal validation rule #45356

merged 2 commits into from Dec 19, 2022

Conversation

RaazPuspa
Copy link
Contributor

This PR adds decimal validation rule to check that an attribute is numeric and consists of decimal part digits between a set of values.

Usage

decimal:min,max: parameter(s) defines minimum and maximum (optional) number of acceptable digits (both inclusive) in decimal part

Examples

  • decimal:1,3: requires values to have one, two or three digits in fractional part
  • decimal:0,3: requires values to have at most three digits in fractional part
  • decimal:3: requires values to have at least three digits in fractional part

@RaazPuspa RaazPuspa changed the title Add decimal validation rule [9.x] Add decimal validation rule Dec 19, 2022
@taylorotwell taylorotwell merged commit 57eca0c into laravel:9.x Dec 19, 2022
@taylorotwell
Copy link
Member

tweaked behavior of rule so that decimal:2 means exactly 2 decimal places.

@taylorotwell
Copy link
Member

Added translation line and replacements.

@willrowe
Copy link
Contributor

Does this allow negative numbers?

@RaazPuspa
Copy link
Contributor Author

@willrowe, I tested the case and found that current implementation does not support validation of signed numbers.

I have updated the validation rule logic allowing signed numeric values. Look #45407 for the changes.

@vlakoff
Copy link
Contributor

vlakoff commented Dec 27, 2022

The dot is unescaped, I think it is unintentional:

preg_match('/^\d*.(\d*)$/', $value, $matches);

(example of value that can erroneously pass currently: 1e12)

However, by just escaping that dot, the case 42 (no decimal part) would not work anymore (it currently works by luck, because the dot makes a backtrack and consumes the last digit).

After some thoughts, I would suggest the following code (includes #45407):

preg_match('/^[+-]?\d*(\.\d*)?$/', $value, $matches);

$decimals = isset($matches[1]) ? (strlen($matches[1]) - 1) : 0;

Also note the following cases are supported by the underlying is_numeric(), and also by this validation rule currently: 42. and .43 (also -42. and -.43). Maybe tests should be added for these.

@vlakoff
Copy link
Contributor

vlakoff commented Dec 27, 2022

And for consistency with the Numeric rule, maybe also support leading/trailing spaces:

preg_match('/^\s*[+-]?\d*(\.\d*)?\s*$/', $value, $matches);

preg_match('/^[+-]?\d*(\.\d*)?$/', trim($value, " \t\n\r\v"), $matches); // variant

(the null-byte character \x0 is not supported by is_numeric(), hence excluding it from the trim() in the variant)

@vlakoff
Copy link
Contributor

vlakoff commented Jan 9, 2023

I could open a PR for this, but I would need your stance on the points I exposed above.

  • Should .42 and 42. values pass?
  • Should leading/trailing spaces pass?

I'd say yes for both. Not because they would arguably be better per se, but for consistency, because is_numeric(), thus the Numeric rule, accept them.

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.

None yet

4 participants