0
votes

I have a input request that can be an integer or an array of integer values.

How can I check validation of that in Laravel? I'm using Laravel 5.5.I know that there is a simple array validation rules that checks an input is an array or not:

array

The field under validation must be a PHP array.

Is there any predefined validation rule or should I write a new one? if yes, How?

1
you can create same custom validation rules, which will check if you input field is and array or just un integer - Yves Kipondo
each array elements should be an integer . how to check it ? - A.B.Developer

1 Answers

0
votes
$input = $request->all();

//Check if input param is an array
if(is_array($input['item']))
{
  //Array validator
  $Validator = Validator::make($input, 
    [
        'item' => 'required', //Array with key 'item' must exist
        'item.*' => 'sometimes|integer', //All values should be integers
    ]);
}
else
{
  //Integer validator
  $Validator = Validator::make($input, 
    [
        'item'   => 'required|integer',
    ]);
}

Use sometimes it applies validation rule only if that input parameter exist. Hope this helps