51 lines
1.4 KiB
PHP
Executable File
51 lines
1.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class StorePeriodRequest extends FormRequest
|
|
{
|
|
public const MAX_RANGE_DAYS = 366;
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'marker_id' => [
|
|
'required',
|
|
'integer',
|
|
Rule::exists('markers', 'id')->where('user_id', $this->user()->id),
|
|
],
|
|
'start_date' => ['required', 'date_format:Y-m-d'],
|
|
'end_date' => ['required', 'date_format:Y-m-d', 'after_or_equal:start_date'],
|
|
];
|
|
}
|
|
|
|
public function withValidator(Validator $validator): void
|
|
{
|
|
$validator->after(function ($validator) {
|
|
if ($validator->errors()->isNotEmpty()) {
|
|
return;
|
|
}
|
|
|
|
$start = CarbonImmutable::parse($this->input('start_date'));
|
|
$end = CarbonImmutable::parse($this->input('end_date'));
|
|
|
|
if ($start->diffInDays($end) + 1 > self::MAX_RANGE_DAYS) {
|
|
$validator->errors()->add('end_date', __('validation.max.string', ['attribute' => __('validation.attributes.end_date'), 'max' => self::MAX_RANGE_DAYS]));
|
|
}
|
|
});
|
|
}
|
|
}
|