43 lines
1.4 KiB
PHP
43 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Enums\MealPostVisibility;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class MealPostsRequest extends FormRequest
|
|
{
|
|
public function rules(): array
|
|
{
|
|
$isCreating = $this->isMethod('post');
|
|
|
|
return [
|
|
'image' => [$isCreating ? 'required_without:image_url' : 'sometimes', 'nullable', 'image', 'max:4096'],
|
|
'image_url' => [$isCreating ? 'required_without:image' : 'sometimes', 'nullable', 'string', 'max:2048'],
|
|
'caption' => ['nullable', 'string', 'max:1000'],
|
|
'calories' => ['nullable', 'integer', 'min:0'],
|
|
'proteins' => ['nullable', 'numeric', 'min:0'],
|
|
'carbs' => ['nullable', 'numeric', 'min:0'],
|
|
'fats' => ['nullable', 'numeric', 'min:0'],
|
|
'title' => [$isCreating ? 'required' : 'sometimes', 'string', 'max:255'],
|
|
'eaten_at' => [$isCreating ? 'required' : 'sometimes', 'date'],
|
|
'visibility' => ['sometimes', Rule::enum(MealPostVisibility::class)],
|
|
];
|
|
}
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'image.max' => "L'image ne doit pas dépasser 4 Mo.",
|
|
'image_url.required_without' => 'Ajoute une image au repas.',
|
|
'visibility' => 'Choisis une visibilité valide.',
|
|
];
|
|
}
|
|
}
|