ldc-legaulois/app/Http/Requests/Bo/GeneralSettings/UpdateGeneralSettingRequest.php

89 lines
2.5 KiB
PHP
Raw Normal View History

2024-04-09 12:31:45 +02:00
<?php
namespace App\Http\Requests\Bo\GeneralSettings;
use App\Enums\Settings\SettingsType;
use App\Traits\Requests\HasPicture;
2024-04-09 12:31:45 +02:00
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
class UpdateGeneralSettingRequest extends FormRequest
{
use HasPicture;
2024-04-09 12:31:45 +02:00
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize(): bool
{
return Gate::check('update', $this->route('general_setting'));
}
/**
* Prepare the data for validation.
*
* @return void
*/
public function prepareForValidation(): void
{
2024-04-19 10:08:36 +02:00
/** @var \App\Models\GeneralSetting $generalSettingModel */
$generalSettingModel = $this->route('general_setting');
if ($generalSettingModel->type === SettingsType::hotNews) {
$this->mergePicture('picture');
$this->merge([
'value' => [
2024-04-16 16:52:34 +02:00
'picture_alt' => $this->input('picture_alt'),
'picture_title' => $this->input('picture_title'),
'picture' => $this->input('picture_title'),
'text' => $this->input('text')
2024-04-19 10:08:36 +02:00
]
]);
2024-04-16 16:52:34 +02:00
return;
}
2024-04-09 12:31:45 +02:00
$this->merge([
'value' => json_decode($this->input('jsonValue', '[]'))
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
2024-04-19 10:08:36 +02:00
/** @var \App\Models\GeneralSetting $generalSettingModel */
$generalSettingModel = $this->route('general_setting');
if ($generalSettingModel->type === SettingsType::hotNews) {
$rules = [
'text' => 'required|string|between:3,255',
'picture_alt' => 'required|string|between:3,255',
'picture_title' => 'required|string|between:3,255',
];
return \array_merge(
$rules,
$this->pictureRules('picture', 200, 150, 200, 150, false),
);
} else {
return [
'jsonValue' => 'required|json',
'value' => 'required'
];
}
2024-04-09 12:31:45 +02:00
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes(): array
{
return [
'value' => trans('validation.custom.value')
];
}
}