Skip to content

Commit 66215ea

Browse files
committed
Restrict choosing child category as parent during update.
1 parent e40664b commit 66215ea

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

app/Http/Requests/UpdateCategoryRequest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Requests;
44

5+
use App\Models\Category;
56
use Illuminate\Foundation\Http\FormRequest;
67

78
class UpdateCategoryRequest extends FormRequest
@@ -23,7 +24,20 @@ public function rules(): array
2324
{
2425
return [
2526
'name' => ['required', 'string'],
26-
'parent_id' => ['nullable', 'exists:categories,id'],
27+
'parent_id' => [
28+
'nullable', 'exists:categories,id',
29+
function(string $attribute, $value, \Closure $fail) {
30+
$id = $this->get('id');
31+
$category = Category::where('id', $id)->first();
32+
33+
$children = Category::getAllChildrenByParent($category);
34+
$ids = array_map(fn($c) => $c->id, $children);
35+
36+
if (in_array($value, $ids)) {
37+
return $fail('You cannot choose category as parent which is already a child of the category.');
38+
}
39+
}
40+
],
2741
'active' => ['required', 'boolean']
2842
];
2943
}

backend/src/views/Categories/CategoryModal.vue

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444
</header>
4545
<form @submit.prevent="onSubmit">
4646
<div class="bg-white px-4 pt-5 pb-4">
47-
<CustomInput class="mb-2" v-model="category.name" label="Name"/>
47+
<CustomInput class="mb-2" v-model="category.name" label="Name" :errors="errors['name']"/>
4848
<CustomInput type="select"
4949
:select-options="parentCategories"
5050
class="mb-2"
5151
v-model="category.parent_id"
52-
label="Parent"/>
53-
<CustomInput type="checkbox" class="mb-2" v-model="category.active" label="Active"/>
52+
label="Parent" :errors="errors['parent_id']"/>
53+
<CustomInput type="checkbox" class="mb-2" v-model="category.active" label="Active" :errors="errors['active']"/>
5454
</div>
5555
<footer class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
5656
<button type="submit"
@@ -89,6 +89,7 @@ const category = ref({
8989
})
9090
9191
const loading = ref(false)
92+
const errors = ref({})
9293
9394
const props = defineProps({
9495
modelValue: Boolean,
@@ -135,6 +136,7 @@ onUpdated(() => {
135136
function closeModal() {
136137
show.value = false
137138
emit('close')
139+
errors.value = {};
138140
}
139141
140142
function onSubmit() {
@@ -150,6 +152,10 @@ function onSubmit() {
150152
closeModal()
151153
}
152154
})
155+
.catch(err => {
156+
loading.value = false;
157+
errors.value = err.response.data.errors
158+
})
153159
} else {
154160
store.dispatch('createCategory', category.value)
155161
.then(response => {
@@ -162,7 +168,7 @@ function onSubmit() {
162168
})
163169
.catch(err => {
164170
loading.value = false;
165-
debugger;
171+
errors.value = err.response.data.errors
166172
})
167173
}
168174
}

0 commit comments

Comments
 (0)