51 lines
1.7 KiB
PHP
51 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Roles;
|
|
|
|
use App\Http\Requests\ApiFormRequest;
|
|
use App\Models\Role;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class RoleUpdateRequest extends ApiFormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return auth()->check();
|
|
}
|
|
|
|
public function rules(): array
|
|
{
|
|
$roleId = (int) (
|
|
$this->route('roleId')
|
|
?? $this->route('id')
|
|
?? $this->input('id')
|
|
?? 0
|
|
);
|
|
$current = $roleId > 0 ? Role::query()->find($roleId) : null;
|
|
|
|
$currentName = strtolower(trim((string) ($current?->name ?? '')));
|
|
$currentSlug = strtolower(trim((string) ($current?->slug ?? '')));
|
|
$incomingName = strtolower(trim((string) $this->input('name', '')));
|
|
$incomingSlug = strtolower(trim((string) $this->input('slug', '')));
|
|
|
|
$nameRules = ['sometimes', 'string', 'min:3', 'max:255'];
|
|
if ($incomingName !== '' && $incomingName !== $currentName) {
|
|
$nameRules[] = Rule::unique('roles', 'name')->ignore($roleId);
|
|
}
|
|
|
|
$slugRules = ['nullable', 'string', 'max:64'];
|
|
if ($incomingSlug !== '' && $incomingSlug !== $currentSlug) {
|
|
$slugRules[] = Rule::unique('roles', 'slug')->ignore($roleId);
|
|
}
|
|
|
|
return [
|
|
'name' => $nameRules,
|
|
'slug' => $slugRules,
|
|
'description' => ['nullable', 'string', 'max:500'],
|
|
'dashboard_route' => ['sometimes', 'string', 'min:1', 'max:255', 'regex:/^[a-z0-9_\\/\\-]+$/'],
|
|
'priority' => ['sometimes', 'integer', 'min:0', 'max:100000'],
|
|
'is_active' => ['sometimes', Rule::in([0, 1, '0', '1', true, false])],
|
|
];
|
|
}
|
|
}
|