73 lines
2.3 KiB
PHP
73 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class FileUploadRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$maxSize = 10240; // 10MB in KB
|
|
|
|
$allowedMimes = [
|
|
'image/jpeg', 'image/png', 'image/gif', 'image/bmp', 'image/svg+xml', 'image/webp',
|
|
'application/pdf',
|
|
'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
'text/plain', 'text/csv',
|
|
'application/zip', 'application/x-rar-compressed', 'application/x-7z-compressed',
|
|
'application/json', 'application/xml',
|
|
];
|
|
|
|
return [
|
|
'file' => 'required|file|max:' . $maxSize . '|mimetypes:' . implode(',', $allowedMimes),
|
|
'module' => 'nullable|string|max:50',
|
|
'purpose' => 'nullable|string|max:50',
|
|
'description' => 'nullable|string|max:500',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom messages for validator errors.
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'file.required' => '请选择要上传的文件',
|
|
'file.file' => '上传的不是有效的文件',
|
|
'file.max' => '文件大小不能超过10MB',
|
|
'file.mimetypes' => '不支持的文件类型',
|
|
'module.max' => '模块名称最多50个字符',
|
|
'purpose.max' => '用途最多50个字符',
|
|
'description.max' => '描述最多500个字符',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'file' => '文件',
|
|
'module' => '模块',
|
|
'purpose' => '用途',
|
|
'description' => '描述',
|
|
];
|
|
}
|
|
} |