mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 12:14:41 +01:00
feat(Form): global errors (#3482)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
@@ -29,7 +29,7 @@ export interface FormEmits<T extends object> {
|
||||
}
|
||||
|
||||
export interface FormSlots {
|
||||
default(props?: {}): any
|
||||
default(props?: { errors: FormError[] }): any
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -121,7 +121,7 @@ const blurredFields = new Set<keyof T>()
|
||||
function resolveErrorIds(errs: FormError[]): FormErrorWithId[] {
|
||||
return errs.map(err => ({
|
||||
...err,
|
||||
id: inputs.value[err.name]?.id
|
||||
id: err?.name ? inputs.value[err.name]?.id : undefined
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -159,12 +159,12 @@ async function _validate(opts: { name?: keyof T | (keyof T)[], silent?: boolean,
|
||||
if (names) {
|
||||
const otherErrors = errors.value.filter(error => !names.some((name) => {
|
||||
const pattern = inputs.value?.[name]?.pattern
|
||||
return name === error.name || (pattern && error.name.match(pattern))
|
||||
return name === error.name || (pattern && error.name?.match(pattern))
|
||||
}))
|
||||
|
||||
const pathErrors = (await getErrors()).filter(error => names.some((name) => {
|
||||
const pattern = inputs.value?.[name]?.pattern
|
||||
return name === error.name || (pattern && error.name.match(pattern))
|
||||
return name === error.name || (pattern && error.name?.match(pattern))
|
||||
}))
|
||||
|
||||
errors.value = otherErrors.concat(pathErrors)
|
||||
@@ -269,6 +269,6 @@ defineExpose<Form<T>>({
|
||||
:class="form({ class: props.class })"
|
||||
@submit.prevent="onSubmitWrapper"
|
||||
>
|
||||
<slot />
|
||||
<slot :errors="errors" />
|
||||
</component>
|
||||
</template>
|
||||
|
||||
@@ -63,7 +63,7 @@ const ui = computed(() => formField({
|
||||
|
||||
const formErrors = inject<Ref<FormError[]> | null>('form-errors', null)
|
||||
|
||||
const error = computed(() => props.error || formErrors?.value?.find(error => error.name === props.name || (props.errorPattern && error.name.match(props.errorPattern)))?.message)
|
||||
const error = computed(() => props.error || formErrors?.value?.find(error => error.name && (error.name === props.name || (props.errorPattern && error.name.match(props.errorPattern))))?.message)
|
||||
|
||||
const id = ref(useId())
|
||||
// Copies id's initial value to bind aria-attributes such as aria-describedby.
|
||||
|
||||
@@ -36,7 +36,7 @@ export type FormSchema<T extends object> =
|
||||
export type FormInputEvents = 'input' | 'blur' | 'change' | 'focus'
|
||||
|
||||
export interface FormError<P extends string = string> {
|
||||
name: P
|
||||
name?: P
|
||||
message: string
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user