ldc-legaulois/app/Http/Requests/Bo/Approaches/StoreApproachRequest.php
2026-09-25 16:25:31 +02:00

75 lines
2.4 KiB
PHP

<?php
namespace App\Http\Requests\Bo\Approaches;
use App\Enums\Approaches\ApproachTypeEnum;
use App\Models\Approach;
use App\Traits\Requests\HasPicture;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rules\Enum;
class StoreApproachRequest extends FormRequest
{
use HasPicture;
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return Gate::check('create', Approach::class);
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation(): void
{
$this->merge([
'type' => intval($this->route()->parameter('approach_type')),
'button_url' => intval($this->route()->parameter('approach_type')) === ApproachTypeEnum::demarche->value()
? $this->input('button_url')
: null,
'published' => $this->boolean('published'),
]);
$this->mergePicture('picture');
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
$rules = [
'type' => ['required', new Enum(ApproachTypeEnum::class)],
'name' => 'required|string|between:3,255',
'picture_alt' => 'required_with:picture|nullable|between:3,255',
'picture_title' => 'required_with:picture|nullable|between:3,255',
'published' => 'required|boolean',
];
return \array_merge(
$rules,
['button_url' => ($this->type === ApproachTypeEnum::demarche->value())
? 'required|string|url'
: 'nullable'],
$this->pictureRules('picture', 100, 100, 100, 100),
);
}
/**
* Get custom attributes for validator errors.
*/
public function attributes(): array
{
return [
'name' => trans('validation.attributes.name'),
'picture' => trans('validation.attributes.image'),
'picture_alt' => trans('validation.custom.attribute_alt'),
'picture_title' => trans('validation.custom.attribute_title'),
'button_url' => trans('validation.custom.button_url'),
'published' => trans('validation.attributes.published'),
];
}
}