Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

akhilmurali's avatar

How to validate array input in Laravel?

I have an array input. But when i try to validate it, validation is not working. My blade:

<td><input type="text" name="exam_name[]" placeholder="Exam Name" class="form-control name_list" /></td> 

My controller validation :

$validations = Validator::make($request->all(),[
            'percentage'=> 'required|array',
            'year_passed'=> 'required|array',
            'institute_name'=> 'required|array',
            'exam_name'=> 'required|array',
],[
'exam_name.required'=> 'Please enter a name',
 ]);
0 likes
14 replies
sos99's avatar

@akhilmurali can you give us the details about exam_name ?

for example: a singular element:

exam_name = [
'title' =>"Algebra",
'level'=>7,
]
$validator = Validator::make($request->all(), [
    'exam_name.title' => ['required', 'string','min:3'],
    'exam_name.level' => ['required','integer'],
]);
...

if ($validator->fails()) {
     return;
   }
 
 // Retrieve the validated input...
  $validated = $validator->validated();

//....

source : https://laravel.com/docs/9.x/validation

1 like
akhilmurali's avatar

@sos99 I have 5 text field with name exam_name.

<td><input type="text" name="exam_name[]" placeholder="Exam Name" class="form-control name_list" /></td> 
<td><input type="text" name="exam_name[]" placeholder="Exam Name" class="form-control name_list" /></td> 
<td><input type="text" name="exam_name[]" placeholder="Exam Name" class="form-control name_list" /></td> 
<td><input type="text" name="exam_name[]" placeholder="Exam Name" class="form-control name_list" /></td> 
<td><input type="text" name="exam_name[]" placeholder="Exam Name" class="form-control name_list" /></td> 
Snapey's avatar

depends what type of field it is

1 like
Snapey's avatar

array is not the correct one to use. if you have multiple inputs with a name of exam_name[] and they are all required then the correct rule is

 `exam_name.•` => 'required'
1 like
akhilmurali's avatar

@MohamedTammam When i validate it with min:1 ,it is not displaying any validation message. But when i use min:2 it is displaying validation message.

MohamedTammam's avatar

@Snapey Why? if he needs at least on element in the array, wouldn't that be the right validation rule?

Snapey's avatar

@MohamedTammam How is min:1 any different from required?

array checks that the element has the specified keys, eg array:name,email. OP does not have any keys.

1 like
Snapey's avatar

I have an array input. But when i try to validate it, validation is not working.

In what way do you want to validate the array? What are you checking?

dd($request->all()) at the start of the controller and check you are getting what was expected

akhilmurali's avatar

@Snapey If input is empty ,value should not enter into db. In my case even if input is empty ,NULL value is entering into DB.

Please or to participate in this conversation.