ldc-loue/app/Http/Requests/Bo/News/StoreNewsRequest.php

83 lines
2.3 KiB
PHP

<?php
namespace App\Http\Requests\Bo\News;
use App\Enums\PublishedStatus;
use App\Traits\Requests\HasPicture;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Kwaadpepper\Enum\Rules\EnumIsValidRule;
/**
* Unicity for fields is handled by model events
*/
class StoreNewsRequest extends FormRequest
{
use HasPicture;
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize()
{
return auth('backend')->check();
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation()
{
$this->merge([
'slug' => Str::slug($this->title),
]);
$this->mergePicture();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return \array_merge([
'title' => 'required|min:6|max:255',
'slug' => 'required|max:255',
'seo_title' => 'required|min:45|max:70',
'seo_description' => 'required|min:50|max:160',
'content' => 'required|string|min:15',
'picture_alt' => 'required_with:picture|nullable|string|min:6|max:255',
'picture_title' => 'required_with:picture|nullable|string|min:6|max:255',
'published' => ['required', new EnumIsValidRule(PublishedStatus::class)],
'tags' => 'sometimes|array',
'tags.*' => 'required|array',
'tags.*.id' => 'required|numeric|exists:tags,id|distinct',
'tags.*.name' => 'required|string',
], $this->pictureRules(300, 300, 300, 300));
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
{
return [
'title' => trans('title'),
'slug' => trans('title'),
'seo_title' => trans('title') . ' seo',
'seo_description' => trans('description') . ' seo',
'content' => trans('content'),
'picture' => trans('vignette'),
'picture_alt' => trans('Texte alternatif de la vignette'),
'picture_title' => trans('Titre de la vignette'),
'published' => trans('published'),
];
}
}