91 lines
2.5 KiB
PHP
91 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Back\DynamicPages;
|
|
|
|
use App\Enums\DynamicPages\PageStatusEnum;
|
|
use App\Http\Requests\Back\UpdateDynamicFieldRequest;
|
|
use App\Models\DynamicPage;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Validation\Rules\Enum;
|
|
use Uwil\LaravelCommon\Rules\WysiwygLength;
|
|
|
|
class StoreDynamicPageRequest extends UpdateDynamicFieldRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return Gate::check('create', DynamicPage::class);
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->merge([
|
|
'config_id' => $this->config_id != 0 ? $this->config_id : null,
|
|
'isLocked' => $this->boolean('isLocked'),
|
|
]);
|
|
if ($this->config_id) {
|
|
parent::prepareForValidation();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$rules = [
|
|
'title' => 'required|string|between:3,255',
|
|
'config_id' => 'sometimes|nullable|numeric|exists:page_configs,id',
|
|
'status' => ['required', new Enum(PageStatusEnum::class)],
|
|
'isLocked' => 'required|boolean',
|
|
];
|
|
if ($this->config_id) {
|
|
$rules = \array_merge(
|
|
parent::rules(),
|
|
$rules,
|
|
);
|
|
} else {
|
|
$rules = \array_merge([
|
|
'content' => ['required', 'string', new WysiwygLength()],
|
|
], $rules);
|
|
}
|
|
return $rules;
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
$attributes = [
|
|
'title' => trans('validation.attributes.title'),
|
|
'config_id' => trans_choice('models.classes.page-config', 1),
|
|
'status' => trans('validation.custom.status'),
|
|
'isLocked' => trans('validation.custom.locked'),
|
|
];
|
|
if ($this->config_id) {
|
|
$attributes = \array_merge(
|
|
parent::attributes(),
|
|
$attributes,
|
|
);
|
|
} else {
|
|
$attributes = \array_merge([
|
|
'content' => trans('validation.attributes.content'),
|
|
], $attributes);
|
|
}
|
|
return $attributes;
|
|
}
|
|
}
|