big update base struktur tahap 1
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\MenuGroup;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class IndexMenuGroupRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
/** Kata kunci untuk mencari nama atau deskripsi menu group. */
|
||||
'search' => ['nullable', 'string', 'max:255'],
|
||||
/** Filter status aktif menu group. */
|
||||
'is_active' => ['nullable', 'boolean'],
|
||||
/** Nomor halaman yang ingin diambil. */
|
||||
'page' => ['nullable', 'integer', 'min:1'],
|
||||
/** Jumlah data per halaman, maksimal 100. */
|
||||
'per_page' => ['nullable', 'integer', 'min:1', 'max:100'],
|
||||
/** Kolom pengurutan data. */
|
||||
'sort_by' => ['nullable', Rule::in(['id', 'name', 'description', 'is_active', 'is_system', 'created_at'])],
|
||||
/** Arah pengurutan data. */
|
||||
'sort_direction' => ['nullable', Rule::in(['asc', 'desc'])],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\MenuGroup;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreMenuGroupRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$isMaster = $this->user()?->isMasterAdmin() ?? false;
|
||||
$tenantId = $isMaster
|
||||
? $this->input('tenant_id')
|
||||
: $this->attributes->get('tenant_id');
|
||||
|
||||
return [
|
||||
'tenant_id' => [
|
||||
$isMaster ? 'nullable' : 'prohibited',
|
||||
'integer',
|
||||
'exists:tenants,id',
|
||||
],
|
||||
'name' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:100',
|
||||
Rule::unique('menu_groups', 'name')
|
||||
->where(fn ($query) => $tenantId
|
||||
? $query->where('tenant_id', $tenantId)
|
||||
: $query->whereNull('tenant_id')),
|
||||
],
|
||||
'description' => [
|
||||
'nullable',
|
||||
'string',
|
||||
],
|
||||
'is_active' => [
|
||||
'nullable',
|
||||
'boolean',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$this->merge([
|
||||
'is_active' => filter_var(
|
||||
$this->is_active,
|
||||
FILTER_VALIDATE_BOOLEAN,
|
||||
FILTER_NULL_ON_FAILURE
|
||||
),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\MenuGroup;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class SyncMenuGroupMenusRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'menus' => [
|
||||
'required',
|
||||
'array',
|
||||
'min:1',
|
||||
],
|
||||
'menus.*.menu_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
'distinct',
|
||||
'exists:menu_lists,id',
|
||||
],
|
||||
'menus.*.can_view' => [
|
||||
'nullable',
|
||||
'boolean',
|
||||
],
|
||||
'menus.*.can_create' => [
|
||||
'nullable',
|
||||
'boolean',
|
||||
],
|
||||
'menus.*.can_update' => [
|
||||
'nullable',
|
||||
'boolean',
|
||||
],
|
||||
'menus.*.can_delete' => [
|
||||
'nullable',
|
||||
'boolean',
|
||||
],
|
||||
'menus.*.can_approve' => [
|
||||
'nullable',
|
||||
'boolean',
|
||||
],
|
||||
'menus.*.can_export' => [
|
||||
'nullable',
|
||||
'boolean',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\MenuGroup;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class SyncUserMenuGroupsRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'assignments' => [
|
||||
'required',
|
||||
'array',
|
||||
'min:1',
|
||||
],
|
||||
'assignments.*.menu_group_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
'exists:menu_groups,id',
|
||||
],
|
||||
'assignments.*.tenant_id' => [
|
||||
'nullable',
|
||||
'integer',
|
||||
'exists:tenants,id',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function withValidator($validator): void
|
||||
{
|
||||
$validator->after(function ($validator) {
|
||||
$assignments = $this->input('assignments', []);
|
||||
$seen = [];
|
||||
|
||||
foreach ($assignments as $index => $assignment) {
|
||||
$menuGroupId = $assignment['menu_group_id'] ?? null;
|
||||
$tenantId = $assignment['tenant_id'] ?? null;
|
||||
$key = $menuGroupId.'|'.($tenantId ?? 'null');
|
||||
|
||||
if (isset($seen[$key])) {
|
||||
$validator->errors()->add(
|
||||
"assignments.$index",
|
||||
'Kombinasi menu_group_id dan tenant_id harus unik.'
|
||||
);
|
||||
}
|
||||
|
||||
$seen[$key] = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\MenuGroup;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateMenuGroupRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$menuGroupId = $this->route('menu_group')?->id;
|
||||
$isMaster = $this->user()?->isMasterAdmin() ?? false;
|
||||
$tenantId = $isMaster
|
||||
? ($this->exists('tenant_id')
|
||||
? $this->input('tenant_id')
|
||||
: $this->route('menu_group')?->tenant_id)
|
||||
: $this->route('menu_group')?->tenant_id;
|
||||
|
||||
return [
|
||||
'tenant_id' => [
|
||||
$isMaster ? 'nullable' : 'prohibited',
|
||||
'integer',
|
||||
'exists:tenants,id',
|
||||
],
|
||||
'name' => [
|
||||
'required',
|
||||
'string',
|
||||
'max:100',
|
||||
Rule::unique('menu_groups', 'name')
|
||||
->where(fn ($query) => $tenantId
|
||||
? $query->where('tenant_id', $tenantId)
|
||||
: $query->whereNull('tenant_id'))
|
||||
->ignore($menuGroupId),
|
||||
],
|
||||
'description' => [
|
||||
'nullable',
|
||||
'string',
|
||||
],
|
||||
'is_active' => [
|
||||
'nullable',
|
||||
'boolean',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$this->merge([
|
||||
'is_active' => filter_var(
|
||||
$this->is_active,
|
||||
FILTER_VALIDATE_BOOLEAN,
|
||||
FILTER_NULL_ON_FAILURE
|
||||
),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user