ldc-volena/app/Http/Requests/Bo/Slides/StoreSlideRequest.php
2026-04-24 15:06:55 +02:00

140 lines
4.7 KiB
PHP

<?php
namespace App\Http\Requests\Bo\Slides;
use App\Enums\Slides\SliderTypeEnum;
use App\Rules\WysiwygLength;
use App\Traits\Requests\HasPicture;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Enum;
class StoreSlideRequest extends FormRequest
{
use HasPicture;
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
{
return auth('backend')->check();
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation(): void
{
$this->merge([
'button_status' => $this->boolean('button_status'),
'button_new_tab' => $this->boolean('button_new_tab'),
]);
$this->mergePicture('picture');
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
$rules = [
'type' => [
'required',
new Enum(SliderTypeEnum::class)
],
'title' => [
'required_unless:type,' . SliderTypeEnum::home->value(),
'nullable',
'string',
'between:3,255'
],
'content' => [
'required_if:type,' . SliderTypeEnum::home->value() . ', ' . SliderTypeEnum::team->value(),
'nullable',
'string',
new WysiwygLength(3, 275),
'max:500'
],
'video_id' => 'nullable|string|regex:%([^"&?\/\s]{11})%i',
'picture_alt' => [
'required_unless:type,' . SliderTypeEnum::team->value(),
'nullable',
'string',
'between:3,255'
],
'picture_title' => [
'required_unless:type,' . SliderTypeEnum::team->value(),
'nullable',
'string',
'between:3,255'
],
'button_label' => 'required_if:button_status,true|nullable|string|between:3,35',
'button_url' => 'required_if:button_status,true|nullable|string|between:3,255',
'button_status' => [
'required_unless:type,' . SliderTypeEnum::home->value() . ', ' . SliderTypeEnum::team->value(),
'nullable',
'boolean'
],
'button_new_tab' => 'required|boolean',
];
return \array_merge(
$rules,
$this->pictureRules('picture', 1920, 1080, 1920, 1080, true)
);
}
/**
* Get custom attributes for validator errors.
*
* @return array<string, string>
*/
public function attributes(): array
{
return [
'type' => trans('validation.custom.type'),
'title' => trans('validation.attributes.title'),
'content' => trans('validation.attributes.content'),
'video_id' => trans('validation.custom.video_id'),
'picture' => trans('validation.custom.picture'),
'picture_alt' => trans('validation.custom.picture_alt'),
'picture_title' => trans('validation.custom.picture_title'),
'button_label' => trans('validation.custom.button_label'),
'button_url' => trans('validation.custom.button_url'),
'button_status' => trans('validation.custom.button_status'),
'button_new_tab' => trans('validation.custom.button_new_tab'),
];
}
/**
* Custom validaton messages.
*
* @return array<string, string>
*/
public function messages(): array
{
return [
'title.required_unless' => __('Le champ :attribute est obligatoire si le slider n\'est pas :name.', [
'attribute' => trans('validation.attributes.title'),
'name' => SliderTypeEnum::home->label()
]),
'content.required_if' => __('Le champ :attribute est obligatoire si le slider est :name1 ou :name2.', [
'attribute' => trans('validation.attributes.content'),
'name1' => SliderTypeEnum::home->label(),
'name2' => SliderTypeEnum::team->label()
]),
'content.max' => __(
'Le champ :attribute ne peut pas contenir plus de :max caractères (balises incluses).',
[
'attribute' => trans('validation.attributes.content'),
'max' => '500',
]
),
];
}
}