New array validation rule required_array_keys

Laravel 8.82 adds a required_array_keys validation rule. The rule checks that all of the specified keys exist in an array.

Valid data that would pass the validation:

1$data = [
2 'baz' => [
3 'foo' => 'bar',
4 'fee' => 'faa',
5 'laa' => 'lee'
6 ],
7];
8 
9$rules = [
10 'baz' => [
11 'array',
12 'required_array_keys:foo,fee,laa',
13 ],
14];
15 
16$validator = Validator::make($data, $rules);
17$validator->passes(); // true

Invalid data that would fail the validation:

1$data = [
2 'baz' => [
3 'foo' => 'bar',
4 'fee' => 'faa',
5 ],
6];
7 
8$rules = [
9 'baz' => [
10 'array',
11 'required_array_keys:foo,fee,laa',
12 ],
13];
14 
15$validator = Validator::make($data, $rules);
16$validator->passes(); // false

Tip given by @AshAllenDesign

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord