menu

Questions & Answers

How to validate multiple inputs in Laravel?

enter image description here

one email is required. and one checkbox should be checked in the first row. if the second email input is filled in the second row then there is also one checkbox should be checked and so on.

@for ($i = 0; $i < 4; $i++)
 <input type="email" id="email" name="email[]">
 <input type="checkbox" name="task[{{ $i }}][]" value="sign">
 <input type="checkbox" name="task[{{ $i }}][]" value="initial">
 <input type="checkbox" name="task[{{ $i }}][]" value="date">
 <input type="checkbox" name="task[{{ $i }}][]" value="cc"
 @endfor

this is the code in my controller its not catch the task field if it's null.

$validatedData = $request->validate([
            'email.0' => 'required_without_all:email.*|email',
            'email.1' => 'nullable|email',
            'email.2' => 'nullable|email',
            'email.3' => 'nullable|email',
            'task.*' => 'required_if:email.*,filled',
        ]);
Comments:
2023-01-17 18:39:18
Use required_with instead, required_if will check the value, which means your field will only be required when email is equal to 'filled', which will never be the case since it's not a valid email. Also you will have to store the values under the same array i think, like : contact.*.email and contact.*.task
2023-01-17 18:39:18
@Lk77 you think ?? it means that you are not sure?
2023-01-17 18:39:18
Yes it means exactly that, i'm sure at 90%, most likely it will not work if it's two different arrays, in the laravel doc the only supported syntax is : 'person.*.first_name' => 'required_with:person.*.last_name', see laravel.com/docs/9.x/validation#validating-nested-array-inpu‌​t
2023-01-17 18:39:18
Have you validated on the front end or not? And what type of validation do you want?
2023-01-17 18:39:18
@SulimanKhan I just want to validate it in my controller
2023-01-17 18:39:18
@AbbasMastan Can you elaborate, please?
2023-01-17 18:39:18
@SulimanKhan Can you please read the upper details.
2023-01-17 18:39:18
@AbbasMastan Sorry? I didn't seen it.
Answers(2) :

You can try this

First, change the field name like below

Here I used data as an array index in order to map the email, sign, date, and initial fields under the same index

@for ($i = 1; $i <= 4; $i++)
 <input type="email" id="email" name="data[{{ $i }}][email]">
 <input type="checkbox" name="data[{{ $i }}][sign]" value="sign">
 <input type="checkbox" name="data[{{ $i }}][initial]" value="initial">
 <input type="checkbox" name="data[{{ $i }}][date]" value="date">
 <input type="checkbox" name="data[{{ $i }}][cc]" value="cc"
 @endfor

Once you pass email in the first box and select the initial option and keep the rest unchanged and add dd($request->all()) in the controller you will output as given below.

array:2 [▼
      "data" => array:4 [▼
        1 => array:2 [▼
          "email" => "someemail@gmail.com"
          "initial" => "initial"
        ]
        2 => array:1 [▼
          "email" => null
        ]
        3 => array:1 [▼
          "email" => null
        ]
        4 => array:1 [▼
          "email" => null
        ]
      ]
    ]

Now add Laravel validation with the given below

$request->validate([
    'data' => 'required|array',
    'data.*.sign' => 'sometimes|required_with:data.*.email',
    'data.*.date' => 'sometimes|required_with:data.*.email',
    'data.*.cc' => 'sometimes|required_with:data.*.email',
    'data.*.initial' => 'required_with:data.*.email',
    'data.*.email' => 'required_with:data.*.sign|required_with:data.*.cc|required_with:data.*.date|required_with:data.*.initial',
 ]);

This will validate the form data both ways around. NOTE: You must add some custom validation to check if non of the values are selected.

I hope you find this helpful.

<?php 
class CourseLevelsRequest
    {
        private array $rules = [
            'course_levels' => 'required|array',
        ];

public function __construct()
{
    $this->makeValidationRules();

    request()->validate($this->rules);

    // your custom logic
}

private function makeValidationRules(): void
{
    collect(request('course_levels'))->each(function ($courseLevelRequest, $index) {
        $this->rules['course_levels.'.$index.'.course_id'] = 'required';
        $this->rules['course_levels.'.$index.'.level'] = 'required|string|max:255';
        $this->rules['course_levels.'.$index.'.programmatic_course_level_name']
            = 'required|string|max:255|unique:course_levels,programmatic_course_level_name,'.$courseLevelRequest['id'];
        $this->rules['course_levels.'.$index.'.fee'] = 'required|numeric';
    });
}

}

Here, I have full control of validation rules