86 lines
2.5 KiB
PHP
86 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo\GeneralSettings;
|
|
|
|
use App\Enums\Settings\SettingsType;
|
|
use App\Traits\Requests\HasPicture;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
class UpdateGeneralSettingRequest extends FormRequest
|
|
{
|
|
use HasPicture;
|
|
|
|
/**
|
|
* 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
|
|
{
|
|
/** @var \App\Models\GeneralSetting $generalSettingModel */
|
|
$generalSettingModel = $this->route('general_setting');
|
|
if ($generalSettingModel->type === SettingsType::hotNews) {
|
|
$this->mergePicture('picture');
|
|
$this->merge([
|
|
'value->picture' => $this->file('picture'),
|
|
'value->text' => $this->input('text'),
|
|
'value->picture_alt' => $this->input('picture_alt'),
|
|
'value->picture_title' => $this->input('picture_title'),
|
|
]);
|
|
return;
|
|
}
|
|
$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
|
|
{
|
|
/** @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, 100, 200, 100, false),
|
|
);
|
|
} else {
|
|
return [
|
|
'jsonValue' => 'required|json',
|
|
'value' => 'required'
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'value' => trans('validation.custom.value')
|
|
];
|
|
}
|
|
}
|