110 lines
4.0 KiB
PHP
110 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class StoreFileRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return Auth::check();
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'file' => 'required|file|max:10240', // Max 10MB
|
|
'file_name' => 'nullable|string|max:255',
|
|
'category' => 'required|string|in:devis,facture,contrat,document,image,autre',
|
|
'client_id' => 'nullable|integer|exists:clients,id',
|
|
'subcategory' => 'nullable|string|max:100',
|
|
'description' => 'nullable|string|max:500',
|
|
'tags' => 'nullable|array|max:10',
|
|
'tags.*' => 'string|max:50',
|
|
'is_public' => 'boolean',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'file' => 'fichier',
|
|
'file_name' => 'nom du fichier',
|
|
'category' => 'catégorie',
|
|
'client_id' => 'client',
|
|
'subcategory' => 'sous-catégorie',
|
|
'description' => 'description',
|
|
'tags' => 'étiquettes',
|
|
'is_public' => 'visibilité publique',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get the error messages for the defined validation rules.
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'file.required' => 'Le fichier est obligatoire.',
|
|
'file.file' => 'Le fichier doit être un fichier valide.',
|
|
'file.max' => 'Le fichier ne peut pas dépasser 10 MB.',
|
|
'file_name.string' => 'Le nom du fichier doit être une chaîne de caractères.',
|
|
'file_name.max' => 'Le nom du fichier ne peut pas dépasser 255 caractères.',
|
|
'category.required' => 'La catégorie est obligatoire.',
|
|
'category.in' => 'La catégorie sélectionnée n\'est pas valide.',
|
|
'client_id.exists' => 'Le client sélectionné n\'existe pas.',
|
|
'subcategory.string' => 'La sous-catégorie doit être une chaîne de caractères.',
|
|
'subcategory.max' => 'La sous-catégorie ne peut pas dépasser 100 caractères.',
|
|
'description.string' => 'La description doit être une chaîne de caractères.',
|
|
'description.max' => 'La description ne peut pas dépasser 500 caractères.',
|
|
'tags.array' => 'Les étiquettes doivent être un tableau.',
|
|
'tags.max' => 'Vous ne pouvez pas ajouter plus de 10 étiquettes.',
|
|
'tags.*.string' => 'Chaque étiquette doit être une chaîne de caractères.',
|
|
'tags.*.max' => 'Chaque étiquette ne peut pas dépasser 50 caractères.',
|
|
'is_public.boolean' => 'La visibilité publique doit être vrai ou faux.',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
// Set default values
|
|
$this->merge([
|
|
'uploaded_by' => $this->user()->id,
|
|
'is_public' => $this->boolean('is_public', false),
|
|
'category' => $this->input('category', 'autre'), // Default category to 'autre' if not provided
|
|
]);
|
|
|
|
// If no file_name provided, use the original file name
|
|
if (!$this->has('file_name') && $this->hasFile('file')) {
|
|
$this->merge([
|
|
'file_name' => $this->file->getClientOriginalName(),
|
|
]);
|
|
}
|
|
|
|
// Extract file information
|
|
if ($this->hasFile('file')) {
|
|
$file = $this->file;
|
|
$this->merge([
|
|
'mime_type' => $file->getMimeType(),
|
|
'size_bytes' => $file->getSize(),
|
|
]);
|
|
}
|
|
}
|
|
}
|