mandant-crm/backend/app/Modules/History/Requests/CreateHistoryEntryRequest.php

42 lines
1.3 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Modules\History\Requests;
use App\Modules\History\Models\HistoryType;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
class CreateHistoryEntryRequest extends FormRequest
{
public function authorize(): bool { return true; }
public function rules(): array
{
return [
'history_entry_type_id' => ['required', 'integer', 'exists:history_types,id'],
'body' => ['required', 'string', 'max:65535'],
// The picker emits a naive local datetime; presence is enforced
// conditionally below based on the chosen type.
'occurred_at' => ['nullable', 'date_format:Y-m-d\TH:i:s'],
];
}
public function withValidator(Validator $validator): void
{
$validator->after(function (Validator $validator) {
$typeId = $this->input('history_entry_type_id');
if (! $typeId) {
return;
}
$type = HistoryType::find($typeId);
if ($type && $type->requires_datetime && ! $this->filled('occurred_at')) {
$validator->errors()->add(
'occurred_at',
'Für diesen Verlaufstyp sind Datum und Uhrzeit erforderlich.'
);
}
});
}
}