in laravel docs there seems to be an integer and a numeric validation rule. i was wondering what the difference between the both was?
2 Answers
Integer is like a whole number without fraction: 2, 256, 2048
.
http://php.net/manual/en/function.is-int.php
Numeric is any number including floating point numbers: 2.478, +0123.45e6
-
In HTML, when using
<input type="number"/>
and validating it on Laravel withinteger
I am getting themust be an integer
error.– PathrosAug 17, 2018 at 16:42 -
1Just a caveat with
integer
: the validation will pass even if the string contains wihtespace before and after the number being checked. For the application, it will produce an error "A non well formed numeric value encountered". Withnumeric
, the application will handle that error properly and ask the user to check their input.– hktangAug 24, 2018 at 3:06 -
1This is misleading. Laravel is not great at strict typing. if $value is set to (bool)true, then is_int returns false, however
filter_var($value, FILTER_VALIDATE_INT) !== false
which Laravel uses for integer validation will return true. So both (bool)true and (int)$x return true according to Laravel's validator. Oct 18, 2019 at 2:43
According to the Laravel's Source Code, both the validations have the following logic.
// For rule 'integer'
protected function validateInteger($attribute, $value)
{
return filter_var($value, FILTER_VALIDATE_INT) !== false;
}
// For rule 'numeric'
protected function validateNumeric($attribute, $value)
{
return is_numeric($value);
}
For more reference dig into the source code of Laravel - here >>