26

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

43

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

3
  • In HTML, when using <input type="number"/> and validating it on Laravel with integer I am getting the must be an integer error.
    – Pathros
    Aug 17, 2018 at 16:42
  • 1
    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.
    – hktang
    Aug 24, 2018 at 3:06
  • 1
    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. Oct 18, 2019 at 2:43
6

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.