ldc-volena/app/Http/Requests/Bo/Strips/StoreStripRequest.php

102 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2023-11-27 11:55:37 +01:00
<?php
namespace App\Http\Requests\Bo\Strips;
use App\Rules\WysiwygLength;
2023-11-27 11:55:37 +01:00
use App\Traits\Requests\HasPicture;
use Illuminate\Foundation\Http\FormRequest;
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
*/
2024-03-20 17:39:24 +01:00
protected function prepareForValidation(): void
2023-11-27 11:55:37 +01:00
{
$this->merge([
'published' => $this->boolean('published'),
2023-11-27 11:55:37 +01:00
]);
if ($this->input('picture')) {
$this->mergePicture('picture');
}
2023-11-27 11:55:37 +01:00
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
2023-11-27 11:55:37 +01:00
*/
public function rules(): array
{
$rules = [
2023-11-27 11:55:37 +01:00
'text' => [
'required',
'string',
new WysiwygLength(3, 90),
2023-11-27 11:55:37 +01:00
],
'news_id' => 'nullable|exists:news,id',
'published' => 'required|boolean',
'publish_at' => 'nullable|date',
'unpublish_at' => 'nullable|date|after:publish_at',
];
if ($this->input('picture')) {
$rules = array_merge(
$rules,
[
'picture_alt' => 'required_with|string|between:3,255',
'picture_title' => 'required_with|string|between:3,255',
],
$this->pictureRules('picture', 100, 65, 100, 65, true),
);
} else {
$rules = array_merge(
$rules,
[
'picto' => [
'required',
new Enum(\App\Enums\Strips\PictoEnum::class)
],
],
);
}
return $rules;
2023-11-27 11:55:37 +01:00
}
/**
* Get custom attributes for validator errors.
*
* @return array<string, string>
2023-11-27 11:55:37 +01:00
*/
public function attributes(): array
{
return [
'picto' => trans('validation.custom.picto'),
'picture' => trans('validation.custom.picture'),
'picture_alt' => trans('validation.custom.alt_attribute'),
'picture_title' => trans('validation.custom.title_attribute'),
'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'),
2023-11-27 11:55:37 +01:00
];
}
}