Laravel - Validation - Rule Array

The field under validation must be a PHP array.

When additional values are provided to the array rule, each key in the input array must be present within the list of values provided to the rule. In the following example, the admin key in the input array is invalid since it is not contained in the list of values provided to the array rule:

    
    use Illuminate\Support\Facades\Validator;
    
    $input = [
        'user' => [
            'name' => 'Taylor Otwell',
            'username' => 'taylorotwell',
            'admin' => true,
        ],
    ];
    
    Validator::make($input, [
        'user' => 'array:username,locale',
    ]);
	

In general, you should always specify the array keys that are allowed to be present within your array. Otherwise, the validator's validate and validated methods will return all of the validated data, including the array and all of its keys, even if those keys were not validated by other nested array validation rules.

If you would like, you may instruct Laravel's validator to never include unvalidated array keys in the "validated" data it returns, even if you use the array rule without specifying a list of allowed keys. To accomplish this, you may call the validator's excludeUnvalidatedArrayKeys method in the boot method of your application's AppServiceProvider. After doing so, the validator will include array keys in the "validated" data it returns only when those keys were specifically validated by nested array rules:

    
    use Illuminate\Support\Facades\Validator;
    
    /**
     * Register any application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::excludeUnvalidatedArrayKeys();
    }