check(); } /** * Prepare the data for validation. * * @return void */ protected function prepareForValidation() { $this->merge([ 'slug' => Str::slug(strip_tags($this->input('title'))), 'status' => $this->boolean('status'), 'ingredients' => $this->input('ingredients') ?? [], 'products' => $this->input('products') ?? [], 'recipes' => $this->input('recipes') ?? [], 'cooking_time' => $this->input('cooking_time') ?? 0, // * For sync relations 'product_list' => \collect($this->products)->map(function ($value) { return \is_array($value) ? ($value['value'] ?? null) : null; })->filter()->all(), 'recipe_list' => \collect($this->recipes)->map(function ($value) { return \is_array($value) ? ($value['value'] ?? null) : null; })->filter()->all(), ]); $this->mergePicture('picture'); $this->mergePicture('sticker'); } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { $rules = [ 'name' => 'required|string|min:3|max:255', 'seo_title' => 'required|min:45|max:70', 'seo_description' => 'required|min:50|max:160', 'picture_alt' => 'required_with:picture|nullable|min:3|max:255', 'picture_title' => 'required_with:picture|nullable|min:3|max:255', 'sticker_alt' => 'required_with:sticker|nullable|min:3|max:255', 'sticker_title' => 'required_with:sticker|nullable|min:3|max:255', 'youtube_video_id' => 'nullable|regex:%^[A-Za-z0-9_-]{11}$%', 'description' => 'required|string|min:15', 'preparation' => 'required|string|min:15', 'ingredients' => 'present|array', 'ingredients.*' => 'required|array', 'ingredients.*.value' => 'required|string|max:10', 'ingredients.*.label' => 'required|string|max:50', 'portions' => 'required|string|min:1|max:15', 'difficulty' => ['required', new Enum(DifficultyEnum::class)], 'cooking_mode' => [ 'required_with:cooking_time', // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed function ($attribute, $value, $fail) { if ($this->cooking_time == 0 and $value != 0) { $fail(\trans(":attribute doit être à :enumValue quand temps de cuisson est à 0.", [ 'enumValue' => Str::lower(CookingModeEnum::withoutCooking->label()) ])); } if ($this->cooking_time != 0 and $value == 0) { $fail(\trans( ":attribute ne peut être à :enumValue quand temps de cuisson est différent de 0.", [ 'enumValue' => Str::lower(CookingModeEnum::withoutCooking->label()) ] )); } }, new Enum(CookingModeEnum::class) ], 'cooking_time' => 'required_unless:cooking_mode,0|numeric|min:0|max:1440', 'preparation_time' => 'required|numeric|min:1|max:1440', 'status' => 'required|boolean', 'products' => [ // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed 'present', 'array', function ($attribute, array $value, $fail) { if (count($value) < 1 || count($value) > 3) { $fail(\trans('Vous devez associer 1 à 3 produits.')); } } ], 'products.*' => 'required|array', 'products.*.value' => 'required|exists:products,id', 'products.*.label' => 'required|string', 'recipes' => [ // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed 'present', 'array', function ($attribute, array $value, $fail) { if (count($value) > 3) { $fail(\trans('Vous devez associer au maximum 3 recettes.')); } } ], 'recipes.*' => 'required|array', 'recipes.*.value' => 'required|exists:recipes,id', 'recipes.*.label' => 'required|string', ]; return \array_merge( $rules, $this->pictureRules('picture', 650, 488, 650, 488, true), $this->pictureRules('sticker', 428, 273, 428, 273, true), ); } /** * Get custom attributes for validator errors. * * @return array */ public function attributes() { return [ 'name' => trans('nom'), 'slug' => trans('slug'), 'seo_title' => trans('title') . ' seo', 'seo_description' => trans('description') . ' seo', 'picture' => trans('visuel'), 'picture_alt' => trans('attribut alt du visuel'), 'picture_title' => trans('titre du visuel'), 'sticker' => trans('vignette'), 'sticker_alt' => trans('attribut alt de la vignette'), 'sticker_title' => trans('titre de la vignette'), 'youtube_video_id' => trans('ID de la vidéo Youtube'), 'description' => trans('description de la recette'), 'preparation' => trans('préparation de la recette'), 'ingredients.*.value' => trans('quantité de l\'ingrédient'), 'ingredients.*.label' => trans('description de l\'ingrédient'), 'products.*.value' => trans('id du produit'), 'products.*.label' => trans('nom du produit'), 'recipes.*.value' => trans('id de la recette'), 'recipes.*.label' => trans('nom de la recette'), 'portions' => trans('nombre de portions'), 'difficulty' => trans('difficulté de la recette'), 'cooking_mode' => trans('mode de cuisson'), 'cooking_time' => trans('Temps de cuisson'), 'preparation_time' => trans('Temps de consommation'), ]; } /** * Get custom messages for validator errors. * * @return array */ public function messages() { return [ 'cooking_time.required_unless' => trans(':attribute est requis sauf si il n\'y a aucun mode de cuisson') ]; } }