我有以下驗證規則,我需要確保streaming_platforms陣列包含正確的資訊。因此,如果鍵為真,則stream_key和stream_url是必需的enable。本stream_key應該是一個字串stream_url應該是一個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);
}
一切正常,但即使enable值為 false ,stream_url 和 stream_key 各自的 URL 和字串驗證規則也會觸發。那么,如果required_if沒有應用,為什么不停止檢查下一個規則呢?
所以我嘗試使用以下資料。
{
"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}
]
}
所以它回傳
stream_platforms.1.stream_url 格式無效。對于 Facebook 行。
我在某處讀到您可以使用sometimes規則執行以下操作,即。但我不知道如何處理陣列型別的資料。
$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;
});
影像形式的真實用例。 
uj5u.com熱心網友回復:
使用exclude_if規則有條件地從驗證中排除該欄位。
$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
請注意,這也會阻止使用該Illuminate\Validation\Validator::validated()方法輸出資料:
dump($v->validated());
輸出:
[
"name" => "testing",
"type" => "public",
"streaming_platforms" => [
[
"stream_url" => "https://www.google.com",
"stream_key" => "abdefghijk",
],
],
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/384616.html
上一篇:查詢生成器Laravel左連接
下一篇:從Laravel的集合中獲取鍵值
