required_if 数组验证规则的验证规则不起作用 | Laravel |验证
required_if validation rule with the array validation rule is not working | Laravel | Validation
我有以下验证规则,我需要在其中确保 streaming_platforms
数组包含正确的信息。因此,如果 enable
键为真,则需要 stream_key
和 stream_url
。 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);
}
一切正常,但是 URL 和 stream_url 和 stream_key 的字符串验证规则各自被触发,即使 enable
值为 false。那么,如果未应用 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}
]
}
所以它 returns
The streaming_platforms.1.stream_url format is invalid. for the
Facebook row.
我在某处读到,您可以执行以下操作,即使用 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;
});
图像形式的真实用例。
使用 exclude_if
规则 conditionally exclude 来自验证的字段。
$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",
],
],
]
我有以下验证规则,我需要在其中确保 streaming_platforms
数组包含正确的信息。因此,如果 enable
键为真,则需要 stream_key
和 stream_url
。 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);
}
一切正常,但是 URL 和 stream_url 和 stream_key 的字符串验证规则各自被触发,即使 enable
值为 false。那么,如果未应用 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}
]
}
所以它 returns
The streaming_platforms.1.stream_url format is invalid. for the Facebook row.
我在某处读到,您可以执行以下操作,即使用 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;
});
图像形式的真实用例。
使用 exclude_if
规则 conditionally exclude 来自验证的字段。
$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",
],
],
]