45 lines
1.4 KiB
PHP
45 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Admin;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class StoreCategoryRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return $this->user()->canManageCategories();
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
$categoryId = $this->route('category')?->id;
|
|
|
|
return [
|
|
'name' => ['required', 'string', 'max:100'],
|
|
'slug' => [
|
|
'nullable',
|
|
'string',
|
|
'max:100',
|
|
'regex:/^[a-z0-9-]+$/',
|
|
Rule::unique('categories', 'slug')->ignore($categoryId)->whereNull('deleted_at'),
|
|
],
|
|
'description' => ['nullable', 'string', 'max:500'],
|
|
'color' => ['nullable', 'string', 'regex:/^#[0-9A-Fa-f]{6}$/'],
|
|
'is_active' => ['boolean'],
|
|
'sort_order' => ['nullable', 'integer', 'min:0'],
|
|
];
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'name.required' => 'Nama kategori wajib diisi.',
|
|
'slug.unique' => 'Slug ini sudah digunakan.',
|
|
'slug.regex' => 'Slug hanya boleh mengandungi huruf kecil, angka, dan tanda (-)',
|
|
'color.regex' => 'Warna mesti dalam format hex (contoh: #3b82f6)',
|
|
];
|
|
}
|
|
}
|