90 lines
2.6 KiB
PHP
90 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Bo\Pictos;
|
|
|
|
use App\Models\Picto;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use App\Rules\WysiwygLength;
|
|
use App\Traits\Requests\HasPicture;
|
|
use Illuminate\Contracts\Validation\Validator;
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
class StorePictoRequest extends FormRequest
|
|
{
|
|
use HasPicture;
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return Gate::check('create', Picto::class);
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->mergePicture('picture');
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$width = (int) ($this->input('picture_width') ?: Picto::MIN_WIDTH);
|
|
$height = (int) ($this->input('picture_height') ?: Picto::MIN_HEIGHT);
|
|
|
|
|
|
$rules = [
|
|
'title' => 'required|string|between:3,255',
|
|
'description' => ['required', 'string', new WysiwygLength(5)],
|
|
'picture_alt' => 'required_with:picture|nullable|between:3,255',
|
|
'picture_title' => 'required_with:picture|nullable|between:3,255',
|
|
'section_id' => 'required|exists:section_pages,id',
|
|
];
|
|
|
|
return array_merge(
|
|
$rules,
|
|
$this->pictureRules('picture', $width, $height, $width, $height)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function attributes()
|
|
{
|
|
return [
|
|
'title' => trans('validation.attributes.title'),
|
|
'description' => trans('validation.attributes.description'),
|
|
'picture' => trans('validation.attributes.image'),
|
|
'picture_alt' => trans('validation.custom.attribute_alt'),
|
|
'picture_title' => trans('validation.custom.attribute_title'),
|
|
];
|
|
}
|
|
|
|
protected function failedValidation(Validator $validator)
|
|
{
|
|
$errors = collect($validator->errors()->all())->join("\n");
|
|
|
|
session()->flash('error', 'Liste des erreurs :' . "\n" . $errors);
|
|
|
|
throw new HttpResponseException(
|
|
response()->json([
|
|
'message' => 'Erreur de validation.',
|
|
'errors' => $validator->errors(),
|
|
], 422)
|
|
);
|
|
}
|
|
}
|