31

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 2

49

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

http://php.net/manual/en/function.is-numeric.php

Sign up to request clarification or add additional context in comments.

4 Comments

In HTML, when using <input type="number"/> and validating it on Laravel with integer I am getting the must be an integer error.
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.
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.
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.
8

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 >>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.