47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
class PermissionRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'module' => 'required|string',
|
|
'label' => [
|
|
'required',
|
|
'string',
|
|
function ($attribute, $value, $fail) {
|
|
$slugName = \Illuminate\Support\Str::slug($value);
|
|
$query = \App\Models\Permission::where('name', $slugName);
|
|
|
|
$permission = $this->route('permission');
|
|
if ($permission) {
|
|
$id = $permission instanceof \App\Models\Permission ? $permission->id : $permission;
|
|
$query->where('id', '!=', $id);
|
|
}
|
|
|
|
if ($query->exists()) {
|
|
$fail(__('A permission with this name already exists.'));
|
|
}
|
|
}
|
|
],
|
|
'description' => 'nullable|string',
|
|
];
|
|
}
|
|
}
|