New-Thanasoft/thanasoft-back/app/Http/Requests/UpdateFileRequest.php
2025-12-01 17:02:01 +03:00

97 lines
3.3 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
class UpdateFileRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
$file = $this->route('file');
// Allow if user owns the file or is admin
return Auth::check() && (
$file->uploaded_by === Auth::id() ||
Auth::user()->hasRole('admin')
);
}
/**
* 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_name' => 'sometimes|string|max:255',
'description' => 'nullable|string|max:500',
'tags' => 'nullable|array|max:10',
'tags.*' => 'string|max:50',
'is_public' => 'boolean',
'category' => 'sometimes|string|in:devis,facture,contrat,document,image,autre',
'client_id' => 'nullable|integer|exists:clients,id',
'subcategory' => 'nullable|string|max:100',
];
}
/**
* Get custom attributes for validator errors.
*/
public function attributes(): array
{
return [
'file_name' => 'nom du fichier',
'description' => 'description',
'tags' => 'étiquettes',
'is_public' => 'visibilité publique',
'category' => 'catégorie',
'client_id' => 'client',
'subcategory' => 'sous-catégorie',
];
}
/**
* Get the error messages for the defined validation rules.
*/
public function messages(): array
{
return [
'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.',
'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.',
'category.string' => 'La catégorie doit être une chaîne de caractères.',
'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.',
];
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
// Only merge fields that are present in the request
$data = [];
if ($this->has('is_public')) {
$data['is_public'] = $this->boolean('is_public');
}
$this->merge($data);
}
}