fn

useField

v0.0.14testeddemo

Bind a single field by path. When rendered under a useForm(or given an explicit form), it reads/writes that form's state; otherwise it runs standalone with its own value, errors, and validation. Returns a writable value, reactive errors/meta, blur/change handlers, and attrs to spread.

Examples

ts
// Within a useForm() component
const { value, errorMessage, attrs } = useField('email', {
  validate: (v) => v.includes('@') || 'Invalid email',
});
// <input v-model="value" v-bind="attrs">
ts
// Standalone (no form ancestor)
const { value, errors } = useField('search', { initialValue: '', schema });

Demo

Loading demo…

Signature

ts
export function useField<T = unknown>(
  path: MaybeRefOrGetter<string>,
  options: UseFieldOptions<T> ={ ... }

Type Parameters

T= unknown

Parameters

ParameterTypeDescription
pathMaybeRefOrGetter<string>The dotted field path (reactive allowed)
options?UseFieldOptions<T>Validators, schema, trigger, explicit form, or standalone initialValue

Returns

UseFieldReturn<T>The field's reactive value, errors, meta, and handlers
PropertyTypeDescription
valueRef<T>The field's writable value (bound to the form path, or local in standalone).
errorsComputedRef<string[]>The field's error messages.
errorMessageComputedRef<string | undefined>The field's first error message, if any.
metaFieldMetaGrouped reactive meta for the field.
handleBlur(event?: Event) => voidBlur handler — marks touched and (re)validates per trigger.
handleChange(value: T, shouldValidate?: boolean) => voidSet the value and optionally validate.
handleInput(event: Event) => voidinput/change DOM handler for native elements.
setValue(value: T) => voidSet the field value programmatically.
setTouched(touched?: boolean) => voidMark the field touched/untouched.
setErrors(message: string | string[] | null) => voidSet or clear (with null) the field's errors.
validate() => Promise<FieldValidationResultDetail>Validate just this field.
reset() => voidReset the field to its initial value.
attrsComputedRef<FieldBindingProps>Props to spread on the field element (v-bind="attrs").