ldc-doux/app/Http/Requests/StorePostRequest.php

75 lines
2.3 KiB
PHP
Raw Normal View History

2021-12-17 17:27:25 +01:00
<?php
namespace App\Http\Requests;
use Carbon\Exceptions\InvalidFormatException;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Carbon;
2022-08-29 17:23:10 +02:00
use Illuminate\Validation\ValidationException;
2021-12-17 17:27:25 +01:00
class StorePostRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
2022-08-11 12:34:27 +02:00
* @return boolean
2021-12-17 17:27:25 +01:00
*/
public function authorize()
2022-08-29 17:23:10 +02:00
{
return auth()->check();
}
/**
* Prepare the data for validation.
*
* @return void
*
* @throws ValidationException When prepare merge fails to parse dates.
*/
protected function prepareForValidation()
2021-12-17 17:27:25 +01:00
{
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(),
'on_top' => $this->on_top ? true : false,
]);
} catch (InvalidFormatException $e) {
2022-08-29 17:23:10 +02:00
$error = ValidationException::withMessages([
'date_start_date' => ['Format de date incorrect'],
'date_end_date' => ['Format de date incorrect'],
]);
throw $error;
2021-12-17 17:27:25 +01:00
}
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'picture_news' => 'required|image|mimes:jpeg,png,jpg|max:1024|dimensions:max_width=380,max_height=298',
2021-12-20 14:02:37 +01:00
'alt_text_picture_news' => 'required|max:255',
2021-12-17 17:27:25 +01:00
'title' => 'required|max:255',
'text' => 'required|min:20',
'date_start' => [
'required',
'date_format:Y-m-d H:i:s',
2022-03-01 18:40:52 +01:00
'before:' . Carbon::now()->parse('2037-12-31')
2021-12-17 17:27:25 +01:00
],
'date_end' => [
'required',
'date_format:Y-m-d H:i:s',
'after:date_start',
'before:' . Carbon::now()->parse('2037-12-31')
],
'picture_home' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:max_width=1030,max_height=239',
2021-12-20 14:02:37 +01:00
'alt_text_picture_home' => 'required|max:255',
2021-12-17 17:27:25 +01:00
'on_top' => 'nullable|boolean',
];
}
}