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
4 Comments
Pathros
In HTML, when using
<input type="number"/> and validating it on Laravel with integer I am getting the must be an integer error.hktang
Just 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". With numeric, the application will handle that error properly and ask the user to check their input.MattWithoos
This 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.Hashim Aziz
This would imply they're mutually exclusive, yet Laravel recommends using both together so this clearly isn't the case and doesn't quite answer the question of what the difference is between them, at least not in 2025.
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 >>