Back to Course |
Laravel Array Validation: All You Need To Know

Bonus: A Few More "Quick Tips"

Once you get the basics of validating arrays in Laravel, you might find cases with more things to validate. In this lesson, we've added smaller tips that might help you.


Field in Array Depends on Another Field

Sometimes in your Requests, you might have a field that's required only if another field is present. For example, if you have a field is_company, then you might want to require company_number too:

In this case, your validation rules will look like this:

public function rules(): array
{
return [
'cv' => ['nullable', 'file'],
'references' => ['required', 'array'],
'references.*.name' => ['required', 'string'],
'references.*.is_company' => ['nullable', 'boolean'],
'references.*.company_number' => ['required_with:references.*.is_company'],
];
}

Note the required_with:references.*.is_company rule. This rule will require the company_number field only if the is_company is present. Otherwise, it will be ignored.


Validating an Array, Not Just Request

While it's not the intended use case, you can validate any array using the Laravel Validator class.

For example, we need to validate the following array:

$array = [
'name' => 'John Doe',
'education' => [
'primary' => 'Primary School',
'secondary' => 'Secondary School',
'university' => 'University',
],
'experience' => [
[
'company' => 'Company 1',
'position' => 'Position 1',
'duration' => 'Duration 1',
],
[
'company' => 'Company 2',
'position' => 'Position 2',
'duration' => 'Duration 2',
],
]
];

And our main focus is to validate the data that's there. To do so, we can use the following code:

$array = [
'name' => 'John Doe',
'education' => [
'primary' => 'Primary School',
'secondary' => 'Secondary School',
'university' => 'University',
],
'experience' => [
[
'company' => 'Company 1',
'position' => 'Position 1',
'duration' => 'Duration 1',
],
[
'company' => 'Company 2',
'position' => 'Position 2',
'duration' => 'Duration 2',
],
]
];
 
$validator = Validator::make($array, [
'name' => ['required', 'string', 'max:200'],
'education' => ['required', 'array'],
'education.primary' => ['required', 'string', 'max:200'],
'education.secondary' => ['required', 'string', 'max:200'],
'education.university' => ['required', 'string', 'max:200'],
'experience' => ['required', 'array'],
'experience.*.company' => ['required', 'string', 'max:200'],
'experience.*.position' => ['required', 'string', 'max:200'],
'experience.*.duration' => ['required', 'string', 'max:200'],
'experience.*.start_date' => ['required', 'date', 'date_format:Y-m-d'],
'experience.*.end_date' => ['nullable', 'date', 'date_format:Y-m-d'],
]);
 
if ($validator->fails()) {
dd($validator->errors());
}
 
dd('success');

Running this code will give us the following output:

This is because our array doesn't have the start_date field. If we add it, we'll get the following output:

$array = [
'name' => 'John Doe',
'education' => [
'primary' => 'Primary School',
'secondary' => 'Secondary School',
'university' => 'University',
],
'experience' => [
[
'company' => 'Company 1',
'position' => 'Position 1',
'duration' => 'Duration 1',
'start_date' => '2020-01-01',
],
[
'company' => 'Company 2',
'position' => 'Position 2',
'duration' => 'Duration 2',
'start_date' => '2020-01-01',
],
]
];

We didn't add the end_date even if it's in our validation. That's because it is nullable. Adding it won't change the result, but it is great to have it there if it's present and invalid.

The Laravel Validator class is flexible, and you can validate more things than just the request. Here's a list of things you can validate:

  • A CSV file
  • 3rd party API response
  • DTO (Data Transfer Object)
  • Class properties
  • And more...

If you know any more tips, please share them with us in the comments below.