Replies: 11 comments
-
While |
Beta Was this translation helpful? Give feedback.
-
@shaedrich Yes, a phone number is a bad example here (it could have spaces, brackets as well). However, this would be true for any numeric value with an |
Beta Was this translation helpful? Give feedback.
-
I was more referring this from a semantic standpoint. A phone number is nothing you would add or subtract for example. It's coincidentally only comprised of numbers without formatting but doesn't need to, that's probably just a technicality. And if it is "a" number then it's two or more numbers in a trench coat most of the time |
Beta Was this translation helpful? Give feedback.
-
Updated! |
Beta Was this translation helpful? Give feedback.
-
Regarding considering the plus sign as part of an integer representation. The unary Reference: https://www.php.net/manual/en/language.operators.arithmetic.php As such, one could say that this code: var_dump(is_int(+913478344)) // bool(true)
var_dump(is_numeric(+913478344)) // bool(true) It is first doing a conversion to an integer (using the On the other hand, the documentation for integer types, available at: https://www.php.net/manual/en/language.types.integer.php States:
So although, the unary |
Beta Was this translation helpful? Give feedback.
-
Thanks for the explanation on unary Even in the case with Furthermore, I investigated this a bit more and found that the error is consistent with I don't yet understand inner workings of It seems like there is a itsy-bitsy 🐛 as PHP defines
Basic maths also tells us, Even with just this as validation the error persists; 'number' => [
'digits_between:10,15',
] BTW, these works as expected; with both 'number' => [
'integer',
] 'number' => [
'numeric',
] I think all the digit rules (mentioned in issue body) should support |
Beta Was this translation helpful? Give feedback.
-
As the docs says:
References:
I would say your expectation is right, as the docs states: "The integer under validation" and not "A string composed of digits". (actually, one description is missing the word "under") On the other hand, in the code, the comment on the corresponding methods can give an expectation of validating a string composed of digits, see: framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php Lines 681 to 695 in b6a2af2 and: framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php Lines 697 to 713 in b6a2af2 The fix is simple, just changing both methods to use the I can send a PR, but I believe this would be considered a breaking change, as this validation rule is old, and some projects could be relying on the "string composed of digits" behavior and not be expecting for a leading sign. Maybe the solution for the 12.x branch is to add a note on the docs stating it validate if the attributes only contain digits, and not imply it accepts any signed integer. Or to add yet another validation rule to account for signed integer lengths. As a workaround, you can create a custom rule, or use the $validator = Validator::make([
'number' => '+12345678901234',
], [
'number' => [
'nullable',
'numeric',
'regex:/^(\d{10,15}|[+-]\d{10,14})$/',
],
]);
dump($validator->passes()); // true Custom rule sample: <?php
namespace App\Rules;
use Illuminate\Contracts\Validation\ValidationRule;
class IntegerLengthBetween implements ValidationRule
{
public function __construct(
private readonly int $minLength,
private readonly int $maxLength,
) {}
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
if (filter_var($value, FILTER_VALIDATE_INT) === false) {
$fail('validation.integer')->translate();
return;
}
$length = strlen(strval($value));
if ($length < $this->minLength || $length > $this->maxLength) {
$fail('validation.digits_between')->translate([
'min' => $this->minLength,
'max' => $this->maxLength,
]);
}
}
} Usage: $validator = Validator::make([
'number' => '+12345678901234',
], [
'number' => [
'nullable',
'numeric',
new IntegerLengthBetween(10, 15),
],
]);
dump($validator->passes()); // true |
Beta Was this translation helpful? Give feedback.
-
Thanks @rodrigopedra I did implement a custom rule for my project, after that I reported this. Felt that this might solve few things. My two cents;
Let me know, if a PR is needed for docs changes, or should I close this issue as is. |
Beta Was this translation helpful? Give feedback.
-
I am not a maintainer, but in my opinion, a PR to the docs is welcome. You can also try sending a PR to master with the proposed changes. |
Beta Was this translation helpful? Give feedback.
-
Strictly speaking, a number doesn't have a length. Numeric strings have lengths. Numbers usually have places (integer, fraction ("decimal"), "significant"). |
Beta Was this translation helpful? Give feedback.
-
You are correct @shaedrich. Thanks for the heads-up. And that would be a great name for a potentially new rules: EDIT: not sure if using "places" as the name of a rule would be correct, as places regarding a number does not include any signs nor decimal separators. A user might interpret the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
11.44.2
PHP Version
8.3
Database Driver & Version
MySQL 9.2.0
Description
I am using these following rules to validate a numbers, patch method;
The docs says
The integer validation must have a length between...
this is true for all digit related rules, like;So, even if I want to validate a numeric input, Laravel is validating integer input.
Moreover:
All other
size
based validation on numeric inputs, tests the value and not the length.Current Situation
So, there seems to have no option to validate length of a numeric input, it seems.
It would be very useful, if
These digit validations, takes into account the
numeric
orinteger
rule mentioned by dev and not overrides it to aninteger
under the hood.framework/src/Illuminate/Validation/Rules/Numeric.php
Line 79 in b6a2af2
Can we not simply do;
instead of,
Furthermore according to PHP's docs;
This number gives true for both,
is_int
andis_numeric
Steps To Reproduce
This would not accept input like:
+916035350000
this or any integer/numeric with+
. And the error message is also kinda wrong.Beta Was this translation helpful? Give feedback.
All reactions