Home > Mobile >  required_if validation rule with the array validation rule is not working | Laravel | Validation
required_if validation rule with the array validation rule is not working | Laravel | Validation

Time:12-18

I have the following validation rule in which I need to sure that the streaming_platforms array includes the correct information. So the stream_key and stream_url are required if the enable key is true. The stream_key should be a string and stream_url should be a URL.

$validator = Validator::make($input, [
    'name' => 'required|string|min:3|max:255',
    'type' => 'required|in:private,public',
    'streaming_platforms.*.stream_url' => 'required_if:streaming_platforms.*.enable,true|url',
    'streaming_platforms.*.stream_key' => 'required_if:streaming_platforms.*.enable,true|string',
], [
    'streaming_platforms.*.stream_url.required_if' => 'The stream url field is required when platform is enable.',
    'streaming_platforms.*.stream_key.required_if' => 'The stream key field is required when platform is enable.',
]);

if ($validator->fails()) {
    return $this->failResponse([
        "message" => $validator->errors()->first(),
        "messages" => $validator->errors(),
    ], 422);
}

Everything is working fine, but the URL and string validation rule for stream_url and stream_key respective is fire even though the enable value is false. So why does not just stop checking the next rules if the required_if is not applied?

So I tried with the following data.

{
    "name":"testing",
    "type":"public",
    "streaming_platforms":[
        {"id":16,"platform":"youtube","enable":true,"stream_url":"https://www.google.com","stream_key":"abdefghijk"},
        {"id":17,"platform":"facebook","enable":false,"stream_url":null,"stream_key":null},
        {"id":18,"platform":"custom","enable":false,"stream_url":null,"stream_key":null}
    ]
}

So it returns

The streaming_platforms.1.stream_url format is invalid. for the Facebook row.

I read somewhere that you can do something like the following i.e by using the sometimes rule. but I don't how can I do with the array type data.

$validator = Validator::make($request->all(), [
    'name' => ['required', 'min:3', 'max:255'],
    'is_public_template' => 'boolean',

    'logo' => ['required_if:is_public_template,true'],
]);
$validator->sometimes('logo', 'required|image|mimes:jpeg,bmp,png,jpg|dimensions:min_width=128,min_height=128', function ($input) {
    return $input->is_public_template;
});

Real use case in form of an image. multiple stream selection.

CodePudding user response:

Use the exclude_if rule to conditionally exclude the field from validation.

$data = [
    "name" => "testing",
    "type" => "public",
    "streaming_platforms" => [
        ["id" => 16, "platform" => "youtube", "enable" => true, "stream_url" => "https://www.google.com", "stream_key" => "abdefghijk"],
        ["id" => 17, "platform" => "facebook", "enable" => false, "stream_url" => null, "stream_key" => null],
        ["id" => 18, "platform" => "custom", "enable" => false, "stream_url" => null, "stream_key" => null],
     ]
];

$rules = [
    'name' => 'required|string|min:3|max:255',
    'type' => 'required|in:private,public',
    'streaming_platforms.*.stream_url' => 'exclude_if:streaming_platforms.*.enable,false|required|url',
    'streaming_platforms.*.stream_key' => 'exclude_if:streaming_platforms.*.enable,false|required|string',
];

$messages = [
    'streaming_platforms.*.stream_url.required' => 'The stream url field is required when platform is enable.',
    'streaming_platforms.*.stream_key.required' => 'The stream key field is required when platform is enable.',
];

$v = Illuminate\Support\Facades\Validator::make($data, $rules, $messages);

dump($v->fails());

// false

Note this also prevents the data from being output with the Illuminate\Validation\Validator::validated() method:

dump($v->validated());

Output:

   [
     "name" => "testing",
     "type" => "public",
     "streaming_platforms" => [
       [
         "stream_url" => "https://www.google.com",
         "stream_key" => "abdefghijk",
       ],
     ],
   ]
  • Related