Home > Mobile >  Laravel - validate input to be in the interval [-1, 10] without 0. not_in not working properly?
Laravel - validate input to be in the interval [-1, 10] without 0. not_in not working properly?

Time:03-04

As the title says, I'm having trouble validating an input field like I said in the title.

I tried it this way:

$request->validate([
            'nota' => 'min:-1|not_in:0|max:10',
        ]);

Basically, I want this "nota" field to have values in the interval [-1, 10] without 0.

But I can still enter 0.

Why doesn't it work and how can I fix it?

CodePudding user response:

You can try like this:

use Illuminate\Validation\Rule;

$request->validate([
    'nota' => [Rule::notIn([0]), 'between:-1,10'],
]);

or generate numbers yourself like:

    $request->validate([
        'nota' => Rule::in(array_diff(range(-1, 10), [0])),
    ]);
  • Related