ldc-doux/app/Http/Requests/StoreSlideRequest.php
2022-08-29 17:23:10 +02:00

78 lines
2.4 KiB
PHP

<?php
namespace App\Http\Requests;
use Carbon\Exceptions\InvalidFormatException;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Carbon;
class StoreSlideRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return boolean
*/
public function authorize()
{
return auth()->check();
}
/**
* Prepare the data for validation.
*
* @return void
*
* @throws ValidationException When prepare merge fails to parse dates.
*/
protected function prepareForValidation()
{
try {
$this->merge([
'date_start' => Carbon::parse("$this->date_start_date $this->date_start_time:00")->toDateTimeString(),
'date_end' => Carbon::parse("$this->date_end_date $this->date_end_time:00")->toDateTimeString(),
]);
} catch (InvalidFormatException $e) {
$error = \Illuminate\Validation\ValidationException::withMessages([
'date_start_date' => ['Format de date incorrect'],
'date_end_date' => ['Format de date incorrect'],
]);
throw $error;
}
$this->merge([
'title' => 'lorem title ' . rand(),
'text' => 'lorem text ' . rand(),
'link_new_tab' => $this->link_new_tab ? true : false
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required|max:255',
'text' => 'sometimes|max:255',
'picture' => 'sometimes|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=1920,height=812',
'alt_text_picture' => 'sometimes|max:255',
'link_label' => 'sometimes|max:255',
'link' => 'sometimes|max:255',
'link_new_tab' => 'required|boolean',
'slider_id' => 'required|exists:sliders,id',
'date_start' => [
'nullable',
'date_format:Y-m-d H:i:s',
'before:' . Carbon::now()->parse('2037-12-31')
],
'date_end' => [
'nullable',
'date_format:Y-m-d H:i:s',
'after:date_start',
'before:' . Carbon::now()->parse('2037-12-31')
],
];
}
}