biniyam20's avatar

Best way to validate range in Laravel?

Hi, looking at the documentation and testing the code there seems to be no way for me to validate a field is between 0 and 499 (inclusive for both).

https://laravel.com/docs/5.7/validation

All of the validation methods that have to do with number comparison, use the length of the numeric input which is not my use case.

So anyways, I decided to alter my use case and be content with 0 to 999 values so I can just use the 1,3 method which will allow values 0 to 999 but it also allows negative values -1 to -999 because it is computing size and not value.

Any suggestions on how to filter for value and not size, and with regards to my specific use case?

Thank you.

//All of these methods validates by size of the input and not value, leaving me no viable alternative to validate based off of value.
between:min,max
digits_between:min,max
lt:field
gt:field
etc...

EDIT: I did find another post in laracasts https://laracasts.com/discuss/channels/general-discussion/negative-number-validation?page=1 where the accepted answer it to use min:0 but I don't believe that works to validate negative numbers because min checks length not value. I also tested using min:0 and my negative numbers make it through.

$v = Validator::make($data, [
    'something' => 'integer|min:0'
]);
0 likes
5 replies
jlrdw's avatar

between:min,max The field under validation must have a size between the given min and max. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.

size:value The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For an array, size corresponds to the count of the array. For files, size corresponds to the file size in kilobytes.

biniyam20's avatar

@jlrdw My question is to validate based on value not size. As you point out in the documentation

The field under validation must have a size matching the given value.
biniyam20's avatar

I ended up realizing there is regex validator method and used the following regex to filter out negative values and kept the allowed range from 0 to 999.

'regex:/^[0-9]+/'
Drfraker's avatar
Drfraker
Best Answer
Level 28

You can easily use a custom rule for this use case: run php artisan make:rule ValidAmount

Rule Code:

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class ValidAmount implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        return $value >= 0 && $value <= 999;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The :attribute must be between 0 and 999.';
    }
}

Use in controller:

use App\Rules\ValidAmount;

$request->validate([
    'field' => [new ValidAmount],
]);

1 like
biniyam20's avatar

Thank you! @drfraker Somehow I glossed over that option in the documentation. Your example was perfect. I was actually using a Form Request class but the custom rule works just as well in there too.

I will definitely keep this new tool I've learned in my toolkit for the future :)

1 like

Please or to participate in this conversation.