vacation-planner/app/Http/Requests/StoreMarkerRequest.php
2026-11-28 12:59:18 +08:00

43 lines
1.0 KiB
PHP
Executable File

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
class StoreMarkerRequest extends FormRequest
{
public const MAX_MARKERS = 20;
public function authorize(): bool
{
return true;
}
/**
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'name' => [
'required',
'string',
'max:50',
Rule::unique('markers', 'name')->where('user_id', $this->user()->id),
],
'color' => ['required', 'string', 'regex:/^#[0-9a-fA-F]{6}$/'],
];
}
public function withValidator(Validator $validator): void
{
$validator->after(function ($validator) {
if ($this->user()->markers()->count() >= self::MAX_MARKERS) {
$validator->errors()->add('name', __('The marker limit has been reached.'));
}
});
}
}