-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Conversation
decimal
validation ruledecimal
validation rule
tweaked behavior of rule so that |
Added translation line and replacements. |
Does this allow negative numbers? |
The dot is unescaped, I think it is unintentional: preg_match('/^\d*.(\d*)$/', $value, $matches); (example of value that can erroneously pass currently: However, by just escaping that dot, the case 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 |
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 |
I could open a PR for this, but I would need your stance on the points I exposed above.
I'd say yes for both. Not because they would arguably be better per se, but for consistency, because |
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 partExamples
decimal:1,3
: requires values to have one, two or three digits in fractional partdecimal:0,3
: requires values to have at most three digits in fractional partdecimal:3
: requires values to have at least three digits in fractional part