feat(division): add division management pages and form validation
- Create new division list and add pages - Implement form validation using zod and error handling - Add useFormErrors composable for form error management - Update division entry form with validation support - Add error styling in main.css
This commit is contained in:
@@ -1,26 +1,33 @@
|
||||
<script setup lang="ts">
|
||||
import type { FormErrors } from '~/composables/useFormErrors'
|
||||
import Block from '~/components/pub/custom-ui/form/block.vue'
|
||||
import Combobox from '~/components/pub/custom-ui/form/combobox.vue'
|
||||
import FieldGroup from '~/components/pub/custom-ui/form/field-group.vue'
|
||||
import Field from '~/components/pub/custom-ui/form/field.vue'
|
||||
import Label from '~/components/pub/custom-ui/form/label.vue'
|
||||
|
||||
const props = defineProps<{ modelValue: any }>()
|
||||
const props = defineProps<{
|
||||
modelValue: any
|
||||
division: {
|
||||
msg: {
|
||||
placeholder: string
|
||||
search: string
|
||||
empty: string
|
||||
}
|
||||
items: {
|
||||
value: string
|
||||
label: string
|
||||
code: string
|
||||
}[]
|
||||
}
|
||||
errors?: FormErrors
|
||||
}>()
|
||||
const emit = defineEmits(['update:modelValue', 'event'])
|
||||
|
||||
const data = computed({
|
||||
get: () => props.modelValue,
|
||||
set: (val) => emit('update:modelValue', val),
|
||||
})
|
||||
|
||||
const parentDivision = [
|
||||
{ value: '1', label: 'Medical', code: 'MED' },
|
||||
{ value: '2', label: 'Nursing', code: 'NUR' },
|
||||
{ value: '3', label: 'Admin', code: 'ADM' },
|
||||
{ value: '4', label: 'Support', code: 'SUP' },
|
||||
{ value: '5', label: 'Education', code: 'EDU' },
|
||||
]
|
||||
const defParentDivision = '---pilih divisi utama'
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -30,25 +37,25 @@ const defParentDivision = '---pilih divisi utama'
|
||||
<Block>
|
||||
<FieldGroup :column="2">
|
||||
<Label>Nama</Label>
|
||||
<Field>
|
||||
<Field id="name" :errors="errors">
|
||||
<Input v-model="data.name" />
|
||||
</Field>
|
||||
</FieldGroup>
|
||||
<FieldGroup :column="2">
|
||||
<Label>Kode</Label>
|
||||
<Field>
|
||||
<Field id="code" :errors="errors">
|
||||
<Input v-model="data.code" />
|
||||
</Field>
|
||||
</FieldGroup>
|
||||
<FieldGroup :column="2">
|
||||
<Label>Divisi</Label>
|
||||
<Field>
|
||||
<Field id="parentId" :errors="errors">
|
||||
<Combobox
|
||||
v-model="data.parentId"
|
||||
:items="parentDivision"
|
||||
:placeholder="defParentDivision"
|
||||
search-placeholder="kode, nama divisi"
|
||||
empty-message="divisi tidak ditemukan."
|
||||
:items="props.division.items"
|
||||
:placeholder="props.division.msg.placeholder"
|
||||
:search-placeholder="props.division.msg.search"
|
||||
:empty-message="props.division.msg.empty"
|
||||
/>
|
||||
</Field>
|
||||
</FieldGroup>
|
||||
|
||||
@@ -1,19 +1,85 @@
|
||||
<script setup lang="ts">
|
||||
import Action from '~/components/pub/custom-ui/nav-footer/ba-dr-su.vue'
|
||||
import * as z from 'zod'
|
||||
import Action from '~/components/pub/custom-ui/nav-footer/ba-su.vue'
|
||||
|
||||
const { errors, setFromZodError, clearErrors } = useFormErrors()
|
||||
|
||||
const data = ref({
|
||||
code: '',
|
||||
name: '',
|
||||
parentId: 0,
|
||||
parentId: '',
|
||||
})
|
||||
|
||||
const division = {
|
||||
msg: {
|
||||
placeholder: '---pilih divisi utama',
|
||||
search: 'kode, nama divisi',
|
||||
empty: 'divisi tidak ditemukan',
|
||||
},
|
||||
items: [
|
||||
{ value: '1', label: 'Medical', code: 'MED' },
|
||||
{ value: '2', label: 'Nursing', code: 'NUR' },
|
||||
{ value: '3', label: 'Admin', code: 'ADM' },
|
||||
{ value: '4', label: 'Support', code: 'SUP' },
|
||||
{ value: '5', label: 'Education', code: 'EDU' },
|
||||
{ value: '6', label: 'Pharmacy', code: 'PHA' },
|
||||
{ value: '7', label: 'Radiology', code: 'RAD' },
|
||||
{ value: '8', label: 'Laboratory', code: 'LAB' },
|
||||
{ value: '9', label: 'Finance', code: 'FIN' },
|
||||
{ value: '10', label: 'Human Resources', code: 'HR' },
|
||||
{ value: '11', label: 'IT Services', code: 'ITS' },
|
||||
{ value: '12', label: 'Maintenance', code: 'MNT' },
|
||||
{ value: '13', label: 'Catering', code: 'CAT' },
|
||||
{ value: '14', label: 'Security', code: 'SEC' },
|
||||
{ value: '15', label: 'Emergency', code: 'EMR' },
|
||||
{ value: '16', label: 'Surgery', code: 'SUR' },
|
||||
{ value: '17', label: 'Outpatient', code: 'OUT' },
|
||||
{ value: '18', label: 'Inpatient', code: 'INP' },
|
||||
{ value: '19', label: 'Rehabilitation', code: 'REB' },
|
||||
{ value: '20', label: 'Research', code: 'RSH' },
|
||||
],
|
||||
}
|
||||
|
||||
const schema = z.object({
|
||||
name: z.string().min(1, 'Nama divisi harus diisi'),
|
||||
code: z.string().min(1, 'Kode divisi harus diisi'),
|
||||
parentId: z.preprocess(
|
||||
(input: unknown) => {
|
||||
if (typeof input === 'string') {
|
||||
// Handle empty string case
|
||||
if (input.trim() === '') {
|
||||
return 0
|
||||
}
|
||||
return Number(input)
|
||||
}
|
||||
|
||||
return input
|
||||
},
|
||||
z.number().refine((num) => num > 0, 'Divisi harus dipilih'),
|
||||
),
|
||||
})
|
||||
|
||||
function onClick(type: string) {
|
||||
if (type === 'cancel') {
|
||||
navigateTo('/human-src/user')
|
||||
navigateTo('/org-src/division')
|
||||
} else if (type === 'draft') {
|
||||
// do something
|
||||
} else if (type === 'submit') {
|
||||
// do something
|
||||
// Clear previous errors
|
||||
clearErrors()
|
||||
|
||||
const requestData = schema.safeParse(data.value)
|
||||
if (!requestData.success) {
|
||||
// Set errors menggunakan composable
|
||||
setFromZodError(requestData.error)
|
||||
|
||||
// Optional: tampilkan toast notification untuk error general
|
||||
console.warn('Form validation failed:', requestData.error)
|
||||
return
|
||||
}
|
||||
|
||||
console.log('Form data valid:', requestData.data)
|
||||
// do something with valid data
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -24,7 +90,7 @@ function onClick(type: string) {
|
||||
<span class="font-semibold">Tambah</span> Divisi
|
||||
</div>
|
||||
<div>
|
||||
<AppDivisonEntryForm v-model="data" />
|
||||
<AppDivisonEntryForm v-model="data" :errors="errors" :division="division" />
|
||||
</div>
|
||||
<div class="my-2 flex justify-end py-2">
|
||||
<Action @click="onClick" />
|
||||
|
||||
Reference in New Issue
Block a user