#The _field_ must be an array... but input says it is?

5 messages · Page 1 of 1 (latest)

wispy coral
#

I have a set of validation rules, with one of them being

"stream" => [
  "array:end_at,viewer_stats,stream_profile,..."
]

Doing dd($this->input()) in the rules method shows us that the stream key is indeed an array

"stream" => array:7 [
  "end_at" => "2022-12-13 07:10:09"
  "viewer_stats" => true
  "end_of_stream_message" => "Hello world"
  "adhoc_protocol" => "RTSP"
  "adhoc_host" => "1.2.3.4:1935"
  "adhoc_username" => "myuser"
  "adhoc_password" => "mypass"
]

And yet I'm getting hit with the following validation error:

"stream": [
    "The stream must be an array."
]
#

Here is the entire FormRequest (I'll split what I think are the important parts out below)

#
public function rules(): array
{
  return [
    'stream'                       => [
      'nullable',
      Rule::prohibitedIf($this->isNotFilled('start_at')),
      'array:end_at,viewer_stats,stream_profile,technician,end_of_stream_message,adhoc_protocol,adhoc_username,adhoc_password',
    ],
    'stream.end_at'                => [
      'date_format:Y-m-d H:i:s',
      'after:start_at',
    ],
    'stream.viewer_stats'          => [
      'nullable',
      'boolean',
    ],
    'stream.stream_profile'        => [
      'string',
      Rule::exists('stream_profiles', 'hash')
        ->withoutTrashed()
        ->where('organization_id', tenant()->getOrganization()->id),
      'prohibits:stream.technician',
    ],
    'stream.technician'            => [
      'string',
      Rule::exists('organizations', 'hash')
        ->withoutTrashed()
        ->where('type', OrganizationTypeEnum::TECHNICIAN),
      'prohibits:stream.stream_profile,stream.adhoc_profile,stream.adhoc_host,stream.adhoc_username,stream.adhoc_password',
    ],
    'stream.end_of_stream_message' => [
      'nullable',
      'string',
    ],
    'stream.adhoc_protocol'        => [
      'nullable',
      'string',
      StreamTypeEnum::rule(),
    ],
    'stream.adhoc_host'            => [
      'nullable',
      'string',
      Rule::requiredIf(in_array($this->input('stream.adhoc_protocol'), [StreamTypeEnum::RTSP, StreamTypeEnum::RTMP_PULL])),
    ],
    'stream.adhoc_username'        => [
      'nullable',
      'string',
      Rule::prohibitedIf(!in_array($this->input('stream.adhoc_protocol'), [StreamTypeEnum::RTSP, StreamTypeEnum::RTMP_PULL])),
    ],
    'stream.adhoc_password'        => [
      'nullable',
      'string',
      Rule::prohibitedIf($this->isNotFilled('stream.adhoc_password')),
      Rule::prohibitedIf(!in_array($this->input('stream.adhoc_protocol'), [StreamTypeEnum::RTSP, StreamTypeEnum::RTMP_PULL])),
    ],
  ];
}
#
public function withValidator(Validator $validator)
{
    $validator->sometimes('stream.end_at', ['required'], fn() => $this->filled('stream'));
    $validator->sometimes('stream.stream_profile', ['required_without_all:stream.technician,stream.adhoc_protocol'], fn() => $this->filled('stream'));
    $validator->after(function ($validator) {
        if ($this->filled('stream.start_at')) {
            $requestedStart = Carbon::parse($this->input('stream.start_at'));
            $requestedEnd   = Carbon::parse($this->input('stream.end_at'));
            if ($requestedStart->diffInMinutes(now()) < config('stream.minimum_provisioning_buffer')) {
                $validator->errors()->add('start_at', 'The stream start is to close to the current time.');
            }
            if ($requestedStart->diffInMinutes($requestedEnd) > config('stream.max_stream_length')) {
                $validator->errors()->add('end_at', 'The stream length must be no more than ' . number_format(config('stream.max_stream_length')) . ' minutes.');
            }
        }
    });
}
#

The data I am sending (as output from a dd($this->input() in the request.

array:7 [
  "venue_id" => 873
  "name" => "My Event"
  "description" => "<p>Hello World</p>"
  "start_at" => "2022-12-13 06:23:41"
  "is_date_hidden" => false
  "is_venue_hidden" => false
  "stream" => array:7 [
    "end_at" => "2022-12-13 07:23:41"
    "viewer_stats" => true
    "end_of_stream_message" => "Hello world"
    "adhoc_protocol" => "RTSP"
    "adhoc_host" => "1.2.3.4:1935"
    "adhoc_username" => "myuser"
    "adhoc_password" => "mypass"
  ]
]