File tree Expand file tree Collapse file tree 2 files changed +25
-5
lines changed
backend/src/views/Categories Expand file tree Collapse file tree 2 files changed +25
-5
lines changed Original file line number Diff line number Diff line change 22
33namespace App \Http \Requests ;
44
5+ use App \Models \Category ;
56use Illuminate \Foundation \Http \FormRequest ;
67
78class 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 }
Original file line number Diff line number Diff line change 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
9191const loading = ref (false )
92+ const errors = ref ({})
9293
9394const props = defineProps ({
9495 modelValue: Boolean ,
@@ -135,6 +136,7 @@ onUpdated(() => {
135136function closeModal () {
136137 show .value = false
137138 emit (' close' )
139+ errors .value = {};
138140}
139141
140142function 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}
You can’t perform that action at this time.
0 commit comments