Skip to content

Commit 8af70de

Browse files
committed
Show correct validation errors in product form
1 parent 66215ea commit 8af70de

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

app/Http/Requests/ProductRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public function rules()
2929
'deleted_images.*' => ['nullable', 'int'],
3030
'image_positions.*' => ['nullable', 'int'],
3131
'categories.*' => ['nullable', 'int', 'exists:categories,id'],
32-
'price' => ['required', 'numeric'],
33-
'quantity' => ['nullable', 'numeric'],
32+
'price' => ['required', 'numeric', 'min:0.01'],
33+
'quantity' => ['nullable', 'numeric', 'min:0'],
3434
'description' => ['nullable', 'string'],
3535
'published' => ['required', 'boolean']
3636
];

backend/src/views/Products/ProductForm.vue

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
<form v-if="!loading" @submit.prevent="onSubmit">
1212
<div class="grid grid-cols-3">
1313
<div class="col-span-2 px-4 pt-5 pb-4">
14-
<CustomInput class="mb-2" v-model="product.title" label="Product Title"/>
15-
<CustomInput type="richtext" class="mb-2" v-model="product.description" label="Description"/>
16-
<CustomInput type="number" class="mb-2" v-model="product.price" label="Price" prepend="$"/>
17-
<CustomInput type="number" class="mb-2" v-model="product.quantity" label="Quantity"/>
18-
<CustomInput type="checkbox" class="mb-2" v-model="product.published" label="Published"/>
19-
<treeselect v-model="product.categories" :multiple="true" :options="options"/>
14+
<CustomInput class="mb-2" v-model="product.title" label="Product Title" :errors="errors['title']"/>
15+
<CustomInput type="richtext" class="mb-2" v-model="product.description" label="Description" :errors="errors['description']"/>
16+
<CustomInput type="number" class="mb-2" v-model="product.price" label="Price" prepend="$" :errors="errors['price']"/>
17+
<CustomInput type="number" class="mb-2" v-model="product.quantity" label="Quantity" :errors="errors['quantity']"/>
18+
<CustomInput type="checkbox" class="mb-2" v-model="product.published" label="Published" :errors="errors['published']"/>
19+
<treeselect v-model="product.categories" :multiple="true" :options="options" :errors="errors['categories']"/>
2020
</div>
2121
<div class="col-span-1 px-4 pt-5 pb-4">
2222
<image-preview v-model="product.images"
@@ -72,10 +72,12 @@ const product = ref({
7272
description: '',
7373
price: null,
7474
quantity: null,
75-
published: null,
75+
published: false,
7676
categories: []
7777
})
7878
79+
const errors = ref({});
80+
7981
const loading = ref(false)
8082
const options = ref([])
8183
@@ -99,6 +101,7 @@ onMounted(() => {
99101
100102
function onSubmit($event, close = false) {
101103
loading.value = true
104+
errors.value = {};
102105
product.value.quantity = product.value.quantity || null
103106
if (product.value.id) {
104107
store.dispatch('updateProduct', product.value)
@@ -113,6 +116,10 @@ function onSubmit($event, close = false) {
113116
}
114117
}
115118
})
119+
.catch(err => {
120+
loading.value = false;
121+
errors.value = err.response.data.errors
122+
})
116123
} else {
117124
store.dispatch('createProduct', product.value)
118125
.then(response => {
@@ -131,7 +138,7 @@ function onSubmit($event, close = false) {
131138
})
132139
.catch(err => {
133140
loading.value = false;
134-
debugger;
141+
errors.value = err.response.data.errors
135142
})
136143
}
137144
}

0 commit comments

Comments
 (0)