A
A
adrenalinruslan2019-03-11 21:18:38
Laravel
adrenalinruslan, 2019-03-11 21:18:38

Checking for the existence of a Request in Laravel?

Guys, help. How to use the Validator in Laravel to check for the existence of a key in an array?

$request = [];

$validator = Validator::make($request, [ 
    'min' => 'nullable',
    'max' => 'nullable',
]);

// nullable - не подходит, т.к если ключ в массиве не существует он его пропустит
// required - тоже не подходит, т.к он проверяет значение равно ли ''

I made this code,
$request['min'] and $request['max'] are not available and the Validator will skip them,
but how to make it check whether such a key even exists in the array and that's it,
even if the value is equal to '', then it will skip it, because it exists.
In general, I need a check like:
if(!isset($request['min'])) return false;
if(!isset($request['max'])) return false;

but in Validator

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
netrox, 2019-03-11
@netrox

if($request->has('param'))

A
Alex Wells, 2019-03-12
@Alex_Wells

required|filled?

I
ivankomolin, 2019-03-12
@ivankomolin

With your statement of the required question, the most correct answer.
Therefore, it is worth deciding what exactly you want to check.
And then go to the documentation and select the desired rules.
https://laravel.com/docs/5.8/validation#available-...
Checking if min, max are required

$validator = Validator::make($request, [ 
      'min' => 'required',
      'max' => 'required',
]);

Checking whether min, max are required and that their value is an integer
$validator = Validator::make($request, [ 
      'min' => 'required|integer',
      'max' => 'required|integer',
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question