Files
ai-web/app/Http/Requests/Admin/CategoryRequest.php

42 lines
1.1 KiB
PHP
Raw Normal View History

2026-02-11 17:28:36 +08:00
<?php
declare(strict_types=1);
namespace App\Http\Requests\Admin;
use App\Models\Category;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class CategoryRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
$categoryId = $this->route('category')?->id;
return [
'type' => ['required', Rule::in($this->typeOptions())],
'name' => ['required', 'string', 'max:120'],
'slug' => ['required', 'string', 'max:190', Rule::unique('categories', 'slug')->ignore($categoryId)],
'description' => ['nullable', 'string'],
'is_active' => ['nullable', 'boolean'],
];
}
/**
* @return array<int, string>
*/
private function typeOptions(): array
{
$defaults = ['tool', 'model', 'news', 'guide'];
$existing = Category::query()->distinct()->pluck('type')->filter()->values()->all();
return array_values(array_unique([...$defaults, ...$existing]));
}
}