127 lines
4.6 KiB
PHP
127 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo\Carousels;
|
|
|
|
use App\Models\Section;
|
|
use App\Rules\IsMaxChildrenReached;
|
|
use App\Rules\WysiwygLength;
|
|
use App\Traits\Requests\HasPicture;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class StoreCarouselRequest 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
|
|
{
|
|
if ($this->input('media.pictures')) {
|
|
foreach ($this->input('media.pictures') as $index => $pictureArray) {
|
|
$this->mergePicture("media.pictures.$index.path");
|
|
}
|
|
}
|
|
|
|
$this->merge([
|
|
'published' => $this->boolean('published'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$rules = [
|
|
'title' => 'required|string|between:3,255',
|
|
'section_id' => [
|
|
'required',
|
|
Rule::exists('sections', 'id'),
|
|
new IsMaxChildrenReached(new Section(), 'carousels', 5)
|
|
],
|
|
'media' => 'required|array',
|
|
'media.pictures.*.alt' => 'sometimes|string|between:3,255',
|
|
'media.pictures.*.title' => 'sometimes|string|between:3,255',
|
|
'media.pictures.*.legend' => [
|
|
'sometimes',
|
|
'string',
|
|
new WysiwygLength(3, 255),
|
|
],
|
|
'published' => 'required|boolean',
|
|
];
|
|
if ($this->input('media')) {
|
|
if (Arr::exists($this->input('media'), 'pictures')) {
|
|
foreach ($this->input('media')['pictures'] as $pictureIndex => $pictureArray) {
|
|
$rules = array_merge(
|
|
$rules,
|
|
[
|
|
"media.pictures.$pictureIndex.alt" => 'required|string|between:3,255',
|
|
"media.pictures.$pictureIndex.title" => 'required|string|between:3,255',
|
|
"media.pictures.$pictureIndex.legend" => [
|
|
'sometimes',
|
|
'string',
|
|
new WysiwygLength(3, 255),
|
|
],
|
|
],
|
|
$this->pictureRules("media.pictures.$pictureIndex.path", 500, 500, 500, 500, false),
|
|
);
|
|
}
|
|
} //end if
|
|
} //end if
|
|
return $rules;
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
$attributes = [
|
|
'title' => trans('validation.attributes.title'),
|
|
];
|
|
if ($this->input('media') && array_key_exists('pictures', $this->input('media'))) {
|
|
foreach ($this->input('media.pictures') as $index => $pictureArray) {
|
|
$attributes = array_merge(
|
|
$attributes,
|
|
[
|
|
"media.pictures.$index.alt" => trans(':field :inter:model', [
|
|
'field' => trans('validation.custom.alt_attribute'),
|
|
'inter' => trans('validation.custom.of_the.vowel'),
|
|
'model' => sprintf('%s n°%d', trans('validation.custom.picture'), $index + 1),
|
|
]),
|
|
"media.pictures.$index.title" => trans(':field :inter:model', [
|
|
'field' => trans('validation.custom.title_attribute'),
|
|
'inter' => trans('validation.custom.of_the.vowel'),
|
|
'model' => sprintf('%s n°%d', trans('validation.custom.picture'), $index + 1),
|
|
]),
|
|
"media.pictures.$index.legend" => trans(':field :inter:model', [
|
|
'field' => trans('validation.custom.legend'),
|
|
'inter' => trans('validation.custom.of_the.vowel'),
|
|
'model' => sprintf('%s n°%d', trans('validation.custom.picture'), $index + 1),
|
|
]),
|
|
],
|
|
);
|
|
} //end foreach
|
|
} //end if
|
|
return $attributes;
|
|
}
|
|
}
|