89 lines
2.5 KiB
PHP
89 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo\News;
|
|
|
|
use App\Traits\Requests\HasPicture;
|
|
use Closure;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Str;
|
|
|
|
class StoreNewsRequest 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()
|
|
{
|
|
$this->merge([
|
|
'published' => $this->published ? true : false
|
|
]);
|
|
$this->mergePicture('picture');
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$rules = [
|
|
'text' => [
|
|
'required',
|
|
'string',
|
|
'min:3',
|
|
// phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
function (string $attribute, mixed $value, Closure $fail) {
|
|
// phpcs:enable
|
|
if (Str::wordCount(\strip_tags($value)) > 80) {
|
|
$fail(trans('validation.max_words', [
|
|
'attribute' => trans('validation.attributes.text'),
|
|
'count' => '80'
|
|
]));
|
|
}
|
|
}
|
|
],
|
|
'picture_alt' => 'required|string|between:3,255',
|
|
'picture_title' => 'required|string|between:3,255',
|
|
'published' => 'required|boolean',
|
|
'publish_at' => 'nullable|date',
|
|
'unpublish_at' => 'nullable|date|after:publish_at',
|
|
|
|
];
|
|
return \array_merge(
|
|
$rules,
|
|
$this->pictureRules('picture', 308, 223, 308, 223, false),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'text' => trans('validation.attributes.text'),
|
|
'picture_alt' => trans('validation.custom.picture_alt'),
|
|
'picture_title' => trans('validation.custom.picture_title'),
|
|
'published' => trans('validation.custom.activated'),
|
|
'publish_at' => trans('validation.custom.publish_at'),
|
|
'unpublish_at' => trans('validation.custom.unpublish_at'),
|
|
];
|
|
}
|
|
}
|