ldc-volena/app/Http/Requests/Bo/Strips/StoreStripRequest.php
2024-03-22 09:23:24 +01:00

85 lines
2.4 KiB
PHP

<?php
namespace App\Http\Requests\Bo\Strips;
use App\Traits\Requests\HasPicture;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Enum;
class StoreStripRequest 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([
'published' => $this->boolean('published'),
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
'picto' => [
'required',
new Enum(\App\Enums\Strips\PictoEnum::class)
],
'text' => [
'required',
'string',
'min:3',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function (string $attribute, mixed $value, \Closure $fail) {
if (Str::wordCount(\strip_tags($value)) > 255) {
$fail(trans('validation.max_words', [
'attribute' => trans('validation.attributes.text'),
'count' => '255'
]));
}
}
],
'news_id' => 'nullable|exists:news,id',
'published' => 'required|boolean',
'publish_at' => 'nullable|date',
'unpublish_at' => 'nullable|date|after:publish_at',
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
{
return [
'picto' => trans('validation.custom.picto'),
'text' => trans('validation.attributes.content'),
'news_id' => trans('models.classes.news'),
'published' => trans('validation.custom.activated'),
'publish_at' => trans('validation.custom.publish_at'),
'unpublish_at' => trans('validation.custom.unpublish_at'),
];
}
}