Files
ai-web/app/Http/Requests/Admin/CategoryRequest.php
jiangdong.cheng aa16c9f8c2
Some checks failed
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
Tests / PHP 8.4 (push) Has been cancelled
init
2026-02-11 17:28:36 +08:00

42 lines
1.1 KiB
PHP

<?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]));
}
}