Laravel 5.4:如何验证一组具有相同名称的文件输入?
Laravel 5.4: How do I validate an array of file inputs with the same name?
我有多个文件输入
<input name="test[]" type="file">
<input name="test[]" type="file">
<input name="test[]" type="file">
我要验证上传的文件数是否不超过2个
我的验证规则是
'test.*' => 'bail|required|max_files_allowed:2|mimetypes:image/jpeg,image/gif,image/png,max:5000'
我的验证规则是
Validator::extend('max_files_allowed', function ($attribute, $value, $parameters, $validator) {
//$attribute = "test.0"
$field_name = explode(".",$attribute)[0];
$files = $validator->getData()[$field_name];
return (count($files) <= $parameters[0]) ? true : false;
},"You reached the max number of files allowed.");
这是\Log::info($validator->getData())
的例子
array (
'test' =>
array (
0 =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => '2018-05-23_1421.png',
'mimeType' => 'image/png',
'size' => 50101,
'error' => 0,
'hashName' => NULL,
)),
1 =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => '2018-07-26_1752.png',
'mimeType' => 'image/png',
'size' => 105617,
'error' => 0,
'hashName' => NULL,
)),
),
)
当我的验证规则失败时,它会打印多条错误消息。
我假设这是因为它针对数组中的每个元素运行验证规则。
我该怎么做才能让错误消息只显示一次?
或者我计算提交文件数量的方法不正确?
您可以 return 并像这样显示您收到的第一条错误消息。
if($validator->fails()){
$error = $validator->errors()->first();
$responseData = [
'status' => 'failure',
'msg' => $error
];
return json_encode($responseData);
}
您可以将验证规则拆分为两个验证规则:
'test' => 'bail|required|max_files_allowed:2'
'test.*' => 'bail|required|mimetypes:image/jpeg,image/gif,image/png,max:5000'
并将您的自定义验证规则更改为:
Validator::extend('max_files_allowed', function ($attribute, $value, $parameters, $validator) {
//$attribute is now "test" not "test.0"
$field_name = $attribute;
$files = $validator->getData()[$field_name];
return (count($files) <= $parameters[0]) ? true : false;
},"You reached the max number of files allowed.");
因此通过此修改,首先单独验证数组,然后使用第二个验证规则验证每个文件。
注意:未经测试,但应该可以。
使用 max and array 强制 test
是一个数组,因此 min 将检查数组中的项目数。
'test' => 'array|max:2',
'test.*' => 'bail|required|mimetypes:image/jpeg,image/gif,image/png,max:5000'
我有多个文件输入
<input name="test[]" type="file">
<input name="test[]" type="file">
<input name="test[]" type="file">
我要验证上传的文件数是否不超过2个
我的验证规则是
'test.*' => 'bail|required|max_files_allowed:2|mimetypes:image/jpeg,image/gif,image/png,max:5000'
我的验证规则是
Validator::extend('max_files_allowed', function ($attribute, $value, $parameters, $validator) {
//$attribute = "test.0"
$field_name = explode(".",$attribute)[0];
$files = $validator->getData()[$field_name];
return (count($files) <= $parameters[0]) ? true : false;
},"You reached the max number of files allowed.");
这是\Log::info($validator->getData())
array (
'test' =>
array (
0 =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => '2018-05-23_1421.png',
'mimeType' => 'image/png',
'size' => 50101,
'error' => 0,
'hashName' => NULL,
)),
1 =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => '2018-07-26_1752.png',
'mimeType' => 'image/png',
'size' => 105617,
'error' => 0,
'hashName' => NULL,
)),
),
)
当我的验证规则失败时,它会打印多条错误消息。
我假设这是因为它针对数组中的每个元素运行验证规则。
我该怎么做才能让错误消息只显示一次?
或者我计算提交文件数量的方法不正确?
您可以 return 并像这样显示您收到的第一条错误消息。
if($validator->fails()){
$error = $validator->errors()->first();
$responseData = [
'status' => 'failure',
'msg' => $error
];
return json_encode($responseData);
}
您可以将验证规则拆分为两个验证规则:
'test' => 'bail|required|max_files_allowed:2'
'test.*' => 'bail|required|mimetypes:image/jpeg,image/gif,image/png,max:5000'
并将您的自定义验证规则更改为:
Validator::extend('max_files_allowed', function ($attribute, $value, $parameters, $validator) {
//$attribute is now "test" not "test.0"
$field_name = $attribute;
$files = $validator->getData()[$field_name];
return (count($files) <= $parameters[0]) ? true : false;
},"You reached the max number of files allowed.");
因此通过此修改,首先单独验证数组,然后使用第二个验证规则验证每个文件。
注意:未经测试,但应该可以。
使用 max and array 强制 test
是一个数组,因此 min 将检查数组中的项目数。
'test' => 'array|max:2',
'test.*' => 'bail|required|mimetypes:image/jpeg,image/gif,image/png,max:5000'