ldc-nature-and-respect/app/Http/Requests/Bo/Recipes/StoreRecipeRequest.php

108 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2023-03-23 09:08:28 +01:00
<?php
namespace App\Http\Requests\Bo\Recipes;
use App\Enums\Recipes\DifficultyEnum;
use App\Traits\Requests\HasPicture;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Enum;
class StoreRecipeRequest extends FormRequest
{
use HasPicture;
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
2023-03-23 09:08:28 +01:00
{
return auth('backend')->check();
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation(): void
2023-03-23 09:08:28 +01:00
{
$this->merge([
'slug' => Str::slug(strip_tags($this->input('name'))),
2024-01-29 16:02:03 +01:00
'status' => $this->boolean('status'),
2023-03-23 09:08:28 +01:00
]);
$this->mergePicture('picture');
$this->mergePicture('picture_thumbnail');
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
2023-03-23 09:08:28 +01:00
{
$rules = [
'seo_title' => 'required|min:3|max:70',
'seo_description' => 'required|min:50|max:160',
'name' => 'required|min:3|max:255',
'slug' => 'unique_translation:recipes',
'picture_alt' => 'required|min:3|max:255',
'picture_title' => 'required|min:3|max:255',
'picture_thumbnail_alt' => 'required|min:3|max:255',
2023-03-23 09:08:28 +01:00
'picture_thumbnail_title' => 'required|min:3|max:255',
'ingredients' => [
2024-07-16 18:21:29 +02:00
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
2023-03-23 09:08:28 +01:00
'present', 'array', function ($attribute, array $value, $fail) {
if (1 > count($value)) {
$fail(\trans('You must fill in at least 1 line of ingredients.'));
}
}
],
'ingredients.*' => 'nullable|array',
'ingredients.*.header' => 'nullable|string|min:2|max:255',
'ingredients.*.value' => 'string|min:2|max:255',
'teaser' => 'required|min:50|max:160',
'steps' => 'required|string|min:15',
'difficulty' => [new Enum(DifficultyEnum::class)],
'duration' => 'required|numeric|between:1,1440',
2025-07-18 12:23:18 +02:00
'tips' => 'required|string|min:15',
'range_id' => 'required|exists:ranges,id',
'status' => 'required|boolean'
2023-03-23 09:08:28 +01:00
];
return \array_merge(
$rules,
2023-05-09 09:14:21 +02:00
$this->pictureRules('picture', 1600, 900, 1600, 900, false),
$this->pictureRules('picture_thumbnail', 430, 242, 430, 242, false),
2023-03-23 09:08:28 +01:00
);
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
{
return [
'seo_title' => trans('title') . ' seo',
'seo_description' => trans('description') . ' seo',
'name' => trans('name'),
'slug' => trans('slug'),
'ingredients' => trans('ingredients'),
2023-03-23 09:08:28 +01:00
'ingredients.*.header' => trans('header in the list of ingredients'),
'ingredients.*.label' => trans('value of ingredients'),
'ingredients.*.value' => trans('value of ingredients'),
'teaser' => trans('teaser'),
'steps' => trans('steps'),
'difficulty' => trans('difficulty'),
'duration' => trans('duration'),
'tips' => trans('tips'),
'range_id' => trans('range'),
'status' => trans('publish')
2023-03-23 09:08:28 +01:00
];
}
}