feat (material): update logic validate
This commit is contained in:
@@ -3,28 +3,13 @@ import Block from '~/components/pub/custom-ui/form/block.vue'
|
|||||||
import FieldGroup from '~/components/pub/custom-ui/form/field-group.vue'
|
import FieldGroup from '~/components/pub/custom-ui/form/field-group.vue'
|
||||||
import Field from '~/components/pub/custom-ui/form/field.vue'
|
import Field from '~/components/pub/custom-ui/form/field.vue'
|
||||||
import Label from '~/components/pub/custom-ui/form/label.vue'
|
import Label from '~/components/pub/custom-ui/form/label.vue'
|
||||||
import { z } from 'zod'
|
|
||||||
|
|
||||||
const props = defineProps<{ modelValue: any }>()
|
const props = defineProps<{ modelValue: any; errors: any }>()
|
||||||
const emit = defineEmits(['update:modelValue', 'event'])
|
const emit = defineEmits(['update:modelValue', 'event'])
|
||||||
|
|
||||||
const schema = z.object({
|
|
||||||
code: z.string(),
|
|
||||||
name: z.string(),
|
|
||||||
type: z.string(),
|
|
||||||
stock: z.preprocess((val) => Number(val), z.number({ invalid_type_error: "Stok harus berupa angka" })),
|
|
||||||
})
|
|
||||||
|
|
||||||
const data = computed({
|
const data = computed({
|
||||||
get: () => props.modelValue,
|
get: () => props.modelValue,
|
||||||
set: (val) => {
|
set: (val) => {
|
||||||
const result = schema.safeParse(val)
|
|
||||||
if (!result.success) {
|
|
||||||
// You can handle the error here, e.g. show a message
|
|
||||||
const errorMessage = result.error.errors[0]?.message ?? 'Validation error occurred'
|
|
||||||
alert(errorMessage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
emit('update:modelValue', val)
|
emit('update:modelValue', val)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -46,12 +31,20 @@ const items = [
|
|||||||
<Input v-model="data.code" />
|
<Input v-model="data.code" />
|
||||||
</Field>
|
</Field>
|
||||||
</FieldGroup>
|
</FieldGroup>
|
||||||
|
<FieldGroup v-if="!!props.errors.code">
|
||||||
|
<Label></Label>
|
||||||
|
<span class="text-red-400 text-sm">{{ props.errors.code }}</span>
|
||||||
|
</FieldGroup>
|
||||||
<FieldGroup :column="1">
|
<FieldGroup :column="1">
|
||||||
<Label>Nama</Label>
|
<Label>Nama</Label>
|
||||||
<Field>
|
<Field>
|
||||||
<Input v-model="data.name" />
|
<Input v-model="data.name" />
|
||||||
</Field>
|
</Field>
|
||||||
</FieldGroup>
|
</FieldGroup>
|
||||||
|
<FieldGroup v-if="!!props.errors.name">
|
||||||
|
<Label></Label>
|
||||||
|
<span class="text-red-400 text-sm">{{ props.errors.name }}</span>
|
||||||
|
</FieldGroup>
|
||||||
<FieldGroup :column="1">
|
<FieldGroup :column="1">
|
||||||
<Label>Item</Label>
|
<Label>Item</Label>
|
||||||
<Field>
|
<Field>
|
||||||
@@ -61,13 +54,13 @@ const items = [
|
|||||||
<FieldGroup :column="1">
|
<FieldGroup :column="1">
|
||||||
<Label>Satuan</Label>
|
<Label>Satuan</Label>
|
||||||
<Field>
|
<Field>
|
||||||
<Select v-model="data.stock" :items="items" placeholder="Pilih item" />
|
<Select v-model="data.uom" :items="items" placeholder="Pilih item" />
|
||||||
</Field>
|
</Field>
|
||||||
</FieldGroup>
|
</FieldGroup>
|
||||||
<FieldGroup :column="1">
|
<FieldGroup :column="1">
|
||||||
<Label>Stok</Label>
|
<Label>Stok</Label>
|
||||||
<Field>
|
<Field>
|
||||||
<Input v-model="data.stock" />
|
<Input v-model="data.stock" type="number" />
|
||||||
</Field>
|
</Field>
|
||||||
</FieldGroup>
|
</FieldGroup>
|
||||||
</Block>
|
</Block>
|
||||||
|
|||||||
@@ -1,11 +1,20 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import Action from '~/components/pub/custom-ui/nav-footer/ba-dr-su.vue'
|
import Action from '~/components/pub/custom-ui/nav-footer/ba-dr-su.vue'
|
||||||
|
import { z, ZodError } from 'zod'
|
||||||
|
|
||||||
|
const errors = ref({})
|
||||||
const data = ref({
|
const data = ref({
|
||||||
|
code: '',
|
||||||
name: '',
|
name: '',
|
||||||
password: '',
|
|
||||||
status: '',
|
|
||||||
type: '',
|
type: '',
|
||||||
|
stock: 0,
|
||||||
|
})
|
||||||
|
|
||||||
|
const schema = z.object({
|
||||||
|
code: z.string().min(1, 'Code must be at least 1 characters long'),
|
||||||
|
name: z.string().min(1, 'Name must be at least 1 characters long'),
|
||||||
|
type: z.string(),
|
||||||
|
stock: z.preprocess((val) => Number(val), z.number({ invalid_type_error: 'Stok harus berupa angka' })),
|
||||||
})
|
})
|
||||||
|
|
||||||
function onClick(type: string) {
|
function onClick(type: string) {
|
||||||
@@ -15,6 +24,30 @@ function onClick(type: string) {
|
|||||||
// do something
|
// do something
|
||||||
} else if (type === 'submit') {
|
} else if (type === 'submit') {
|
||||||
// do something
|
// do something
|
||||||
|
const input = data.value
|
||||||
|
console.log(input)
|
||||||
|
const errorsParsed: any = {}
|
||||||
|
try {
|
||||||
|
const result = schema.safeParse(input)
|
||||||
|
if (!result.success) {
|
||||||
|
// You can handle the error here, e.g. show a message
|
||||||
|
const errorsCaptures = result?.error?.errors || []
|
||||||
|
const errorMessage = result.error.errors[0]?.message ?? 'Validation error occurred'
|
||||||
|
errorsCaptures.forEach((value: any) => {
|
||||||
|
const keyName = value?.path?.length > 0 ? value.path[0] : 'key'
|
||||||
|
errorsParsed[keyName as string] = value.message || ''
|
||||||
|
})
|
||||||
|
console.log(errorMessage)
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof ZodError) {
|
||||||
|
const jsonError = e.flatten()
|
||||||
|
console.log(JSON.stringify(jsonError, null, 2))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setTimeout(() => {
|
||||||
|
errors.value = errorsParsed
|
||||||
|
}, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -24,7 +57,7 @@ function onClick(type: string) {
|
|||||||
<Icon name="i-lucide-paint-bucket" class="me-2" />
|
<Icon name="i-lucide-paint-bucket" class="me-2" />
|
||||||
<span class="font-semibold">Tambah</span> BMHP
|
<span class="font-semibold">Tambah</span> BMHP
|
||||||
</div>
|
</div>
|
||||||
<AppMaterialEntryForm v-model="data" />
|
<AppMaterialEntryForm v-model="data" :errors="errors" />
|
||||||
<div class="my-2 flex justify-end py-2">
|
<div class="my-2 flex justify-end py-2">
|
||||||
<Action @click="onClick" />
|
<Action @click="onClick" />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user