Skip to content

useForm ​

Top-level form composable. Manages form model state, submission, schema watching, server-side error binding, and orchestrates input handleInput / handleClick callbacks.

File: vue/src/js/hooks/useForm.js


Props Factory ​

js
import { makeFormProps } from '@/hooks/useForm'
PropTypeDefaultDescription
modelValueObject{}The form's data model (v-model)
schemaObject{}Full schema definition — drives field list, rules, and layout
endpointString''API endpoint for form submission
methodString'post'HTTP method (post, put, patch)
idStringautoHTML form id attribute
resetOnSuccessBooleanfalseReset the model to defaults after a successful submission
redirectOnSuccessStringnullURL to redirect to after success
formActionsArray|Object[]Additional action button definitions
hideDefaultActionsBooleanfalseHide the default submit/cancel buttons
submitTextStringt('Save')Label for the primary submit button
cancelTextStringt('Cancel')Label for the cancel button
disabledBooleanfalseDisable all inputs in the form
readonlyBooleanfalseMake all inputs read-only

Usage ​

js
import { useForm, makeFormProps } from '@/hooks/useForm'

const props = defineProps(makeFormProps())
const emit = defineEmits(['update:modelValue', 'success', 'error'])

const {
  model,
  saveForm,
  submit,
  handleInput,
  handleClick,
  setSchemaErrors,
  resetSchemaErrors,
  createSchema,
  validModel,
  formLoading
} = useForm(props, emit)

Returns ​

State ​

NameTypeDescription
modelRef<Object>Reactive copy of the form's data model
formLoadingComputedRef<Boolean>true while a submission is in progress
validModelRef<Boolean|null>Vuetify form validity state (null = not yet validated)

Methods ​

NameSignatureDescription
saveForm() => PromiseValidate, then submit via formApi.post/put/patch
submit() => voidTrigger form validation and call saveForm
handleInput(key, value) => voidCalled by child inputs to update model[key]
handleClick(action) => voidDispatch a form-level action button click
setSchemaErrors(errors: Object) => voidMap server validation errors back onto schema fields
resetSchemaErrors() => voidClear all server-side error messages from the schema
createSchema(definition) => ObjectNormalize and hydrate a raw schema definition into a resolved schema

Watchers ​

  • Watches props.modelValue — syncs external model changes into the internal model.
  • Watches props.schema — re-runs createSchema when the schema definition changes.

Server-side Errors ​

After a failed submission, call setSchemaErrors(errors) with the Laravel validation error bag:

js
setSchemaErrors({
  name: ['The name field is required.'],
  email: ['The email has already been taken.']
})

Each field matching a key in errors will display the first error message beneath the input.

See Also ​