ldc-legaulois/app/Http/Requests/StoreSubRange.php
2022-02-10 16:14:58 +01:00

57 lines
1.8 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Lib\Utils;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
class StoreSubRange extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize()
{
$this->merge([
'back_link_display' => $this->back_link_display ? true : false,
'slug' => Str::slug($this->name)
]);
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string|min:2|max:28',
'slug' => 'required|unique:sub_ranges,slug',
'picture' => 'sometimes|image|max:2048|mimes:png,jpg,jpeg|dimensions:height=170,width=170',
'picture_alt' => 'required_with:picture|string|min:10|max:255',
'title' => 'required|string|min:10|max:255',
'subtitle' => 'required|string|min:10|max:255',
'content' => 'required|string|min:20',
'back_link_display' => 'required|boolean',
'back_link_url' => [
'nullable',
'url',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
try {
Utils::assertNoInternalUrls('Url du lien', $value);
} catch (ValidationException $e) {
$fail(collect($e->errors())->first()[0]);
}
}
],
'back_link_text' => 'nullable|string|min:2|max:20',
];
}
}