useServerErrors
Turns a Laravel validation error (HTTP 422, { message, errors: { field: string[] } }) into per-field error messages, and shows the errors that no field displays as a toast.
useForm uses it internally and returns the same four members, so you need it directly only for forms that do not use useForm.
Registered only when we.form.enabled is true (default).
Usage
const { serverErrors, checkServerErrors, clearServerErrors } = useServerErrors()
async function save() {
try {
await useApi().$post('users', model.value)
} catch (error) {
await checkServerErrors(error)
}
}<v-text-field v-model="model.email" :error-messages="serverErrors.email" />Type Signature
function useServerErrors<T>(): {
serverErrors: { [K in keyof T]?: string[] }
untrackedServerErrors: ComputedRef<{ [field: string]: string[] }>
checkServerErrors: (
error: unknown,
options?: { scroll?: boolean | string, notifyUntracked?: boolean },
) => Promise<boolean>
clearServerErrors: () => void
}Return Value
| Property | Type | Description |
|---|---|---|
serverErrors | reactive object | Errors by field name. Reading a field (e.g. in the template) marks it as tracked |
untrackedServerErrors | ComputedRef<object> | Errors of fields that were never read from serverErrors |
checkServerErrors | (error, options?) => Promise<boolean> | Clears previous errors, decodes error and resolves true if it was a 422 |
clearServerErrors | () => void | Removes all errors and forgets tracked fields |
checkServerErrors options
| Option | Type | Default | Description |
|---|---|---|---|
scroll | boolean | string | '.v-input.error--text' | After the next tick, scrolls the first element matching the selector into view (true uses the default selector, false disables) |
notifyUntracked | boolean | true | If some errors are untracked, shows the error messages in one useNotify().error toast |
WARNING
Passing options replaces both defaults: { scroll: false } also disables the toast unless you add notifyUntracked: true.
How tracking works
serverErrors is a proxy: any field read through it is remembered as displayed. After a 422 the component re-renders, reading the fields bound in the template; on the next tick checkServerErrors checks whether some error has no bound field and, if so, shows a single toast so the user still sees it.
Non-422 errors are not decoded (checkServerErrors resolves false); useApi already shows a toast for them, except 404.