Improve support attachment validation errors

This commit is contained in:
2026-05-16 22:19:55 +03:30
parent e633805ed3
commit 95d14f2cec
8 changed files with 153 additions and 19 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Http\Requests;
use App\Models\SupportConversation;
use App\Rules\SupportAttachmentFile;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
@@ -23,7 +24,7 @@ class StoreSupportConversationRequest extends FormRequest
'subject' => ['required', 'string', 'max:120'],
'body' => ['required', 'string', 'max:2000'],
'attachments' => ['nullable', 'array', 'max:3'],
'attachments.*' => ['file', 'max:10240', 'mimes:jpg,jpeg,png,webp,gif,pdf,txt,log,json,zip'],
'attachments.*' => [new SupportAttachmentFile],
];
}
}

View File

@@ -2,6 +2,7 @@
namespace App\Http\Requests;
use App\Rules\SupportAttachmentFile;
use Illuminate\Foundation\Http\FormRequest;
class StoreSupportMessageRequest extends FormRequest
@@ -12,14 +13,14 @@ class StoreSupportMessageRequest extends FormRequest
}
/**
* @return array<string, array<int, string>>
* @return array<string, array<int, mixed>>
*/
public function rules(): array
{
return [
'body' => ['required', 'string', 'max:2000'],
'attachments' => ['nullable', 'array', 'max:3'],
'attachments.*' => ['file', 'max:10240', 'mimes:jpg,jpeg,png,webp,gif,pdf,txt,log,json,zip'],
'attachments.*' => [new SupportAttachmentFile],
];
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Validator;
use Illuminate\Translation\PotentiallyTranslatedString;
class SupportAttachmentFile implements ValidationRule
{
private const MAX_UPLOAD_KILOBYTES = 204800;
private const ALLOWED_MIME_TYPES = [
'image/*',
'video/*',
'audio/*',
'application/pdf',
'text/plain',
'text/csv',
'application/csv',
'application/json',
'application/x-ndjson',
'text/json',
'application/xml',
'text/xml',
'text/markdown',
'application/x-yaml',
'text/yaml',
'application/zip',
'application/x-zip-compressed',
'application/x-7z-compressed',
'application/vnd.rar',
'application/x-rar-compressed',
'application/x-tar',
'application/gzip',
'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',
'application/rtf',
'text/rtf',
'application/vnd.oasis.opendocument.text',
'application/vnd.oasis.opendocument.spreadsheet',
'application/vnd.oasis.opendocument.presentation',
];
/**
* Run the validation rule.
*
* @param Closure(string, ?string=): PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$validator = Validator::make(
['attachment' => $value],
[
'attachment' => [
'file',
'max:'.self::MAX_UPLOAD_KILOBYTES,
'mimetypes:'.implode(',', self::ALLOWED_MIME_TYPES),
],
],
[
'attachment.file' => 'Attachment upload failed. Try the file again or choose a different file.',
'attachment.max' => 'Attachments must be 200 MB or smaller.',
'attachment.mimetypes' => 'Unsupported attachment type. Upload media, PDF, text/data, Office, OpenDocument, or archive files.',
],
);
if ($validator->fails()) {
$fail($validator->errors()->first('attachment'));
}
}
}