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:
@@ -0,0 +1,73 @@
|
||||
# useFormErrors Composable
|
||||
|
||||
Composable untuk menangani form validation errors seperti Laravel. Mengkonversi ZodError menjadi format yang mudah digunakan di template.
|
||||
|
||||
## Penggunaan
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```typescript
|
||||
// Di component parent (entry.vue)
|
||||
const { errors, setFromZodError, clearErrors } = useFormErrors()
|
||||
|
||||
// Validasi dengan Zod
|
||||
const result = schema.safeParse(data.value)
|
||||
if (!result.success) {
|
||||
setFromZodError(result.error)
|
||||
return
|
||||
}
|
||||
```
|
||||
|
||||
### Di Template
|
||||
|
||||
```vue
|
||||
<!-- Pass errors ke form component -->
|
||||
<AppDivisonEntryForm v-model="data" :errors="errors" />
|
||||
```
|
||||
|
||||
### Di Form Component
|
||||
|
||||
```vue
|
||||
<script setup>
|
||||
import type { FormErrors } from '~/composables/useFormErrors'
|
||||
|
||||
const props = defineProps<{
|
||||
modelValue: any
|
||||
errors?: FormErrors
|
||||
}>()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<!-- Setiap Field harus memiliki id yang sesuai dengan field name -->
|
||||
<Field id="name" :errors="errors">
|
||||
<Input v-model="data.name" />
|
||||
</Field>
|
||||
</template>
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### Methods
|
||||
|
||||
- `setFromZodError(zodError)` - Set errors dari ZodError
|
||||
- `setErrors(errors)` - Set errors manual
|
||||
- `setError(field, message)` - Set error untuk field tertentu
|
||||
- `clearError(field)` - Hapus error untuk field tertentu
|
||||
- `clearErrors()` - Hapus semua errors
|
||||
- `hasError(field)` - Cek apakah ada error untuk field
|
||||
- `getError(field)` - Ambil error message untuk field
|
||||
|
||||
### Computed Properties
|
||||
|
||||
- `hasErrors` - Boolean apakah ada error
|
||||
- `errorMessages` - Array semua error messages
|
||||
- `firstError` - Error pertama (untuk alert general)
|
||||
|
||||
## Field Component
|
||||
|
||||
Field component akan otomatis menampilkan error jika:
|
||||
1. Field memiliki `id` prop yang sesuai dengan field name
|
||||
2. Field menerima `errors` prop
|
||||
3. Ada error untuk field tersebut di dalam errors object
|
||||
|
||||
Error akan ditampilkan dengan class `.field-error-info` yang sudah di-style dengan warna merah.
|
||||
@@ -0,0 +1,115 @@
|
||||
import type { ZodError } from 'zod'
|
||||
|
||||
export interface XError {
|
||||
message: string
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
export interface FormErrors {
|
||||
[field: string]: XError
|
||||
}
|
||||
|
||||
/**
|
||||
* Composable untuk menangani form validation errors seperti Laravel
|
||||
* Mengkonversi ZodError menjadi format yang mudah digunakan di template
|
||||
*/
|
||||
export function useFormErrors() {
|
||||
const errors = ref<FormErrors>({})
|
||||
|
||||
/**
|
||||
* Set errors dari ZodError
|
||||
*/
|
||||
function setFromZodError(zodError: ZodError) {
|
||||
const newErrors: FormErrors = {}
|
||||
|
||||
zodError.errors.forEach((error) => {
|
||||
const field = error.path.join('.')
|
||||
newErrors[field] = {
|
||||
message: error.message,
|
||||
code: error.code,
|
||||
path: error.path,
|
||||
}
|
||||
})
|
||||
|
||||
errors.value = newErrors
|
||||
}
|
||||
|
||||
/**
|
||||
* Set errors manual (untuk error dari API response)
|
||||
*/
|
||||
function setErrors(newErrors: FormErrors) {
|
||||
errors.value = newErrors
|
||||
}
|
||||
|
||||
/**
|
||||
* Set error untuk field tertentu
|
||||
*/
|
||||
function setError(field: string, message: string, extra: Record<string, any> = {}) {
|
||||
errors.value[field] = {
|
||||
message,
|
||||
...extra,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hapus error untuk field tertentu
|
||||
*/
|
||||
function clearError(field: string) {
|
||||
delete errors.value[field]
|
||||
}
|
||||
|
||||
/**
|
||||
* Hapus semua errors
|
||||
*/
|
||||
function clearErrors() {
|
||||
errors.value = {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cek apakah ada error untuk field tertentu
|
||||
*/
|
||||
function hasError(field: string): boolean {
|
||||
return !!errors.value[field]
|
||||
}
|
||||
|
||||
/**
|
||||
* Ambil error message untuk field tertentu
|
||||
*/
|
||||
function getError(field: string): string | null {
|
||||
return errors.value[field]?.message || null
|
||||
}
|
||||
|
||||
/**
|
||||
* Cek apakah ada error apapun
|
||||
*/
|
||||
const hasErrors = computed(() => Object.keys(errors.value).length > 0)
|
||||
|
||||
/**
|
||||
* Ambil semua error messages sebagai array
|
||||
*/
|
||||
const errorMessages = computed(() =>
|
||||
Object.values(errors.value).map(error => error.message),
|
||||
)
|
||||
|
||||
/**
|
||||
* Ambil error pertama (untuk menampilkan alert general)
|
||||
*/
|
||||
const firstError = computed(() => {
|
||||
const firstKey = Object.keys(errors.value)[0]
|
||||
return firstKey ? errors.value[firstKey] : null
|
||||
})
|
||||
|
||||
return {
|
||||
errors: readonly(errors),
|
||||
setFromZodError,
|
||||
setErrors,
|
||||
setError,
|
||||
clearError,
|
||||
clearErrors,
|
||||
hasError,
|
||||
getError,
|
||||
hasErrors,
|
||||
errorMessages,
|
||||
firstError,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user