Skip to main content

useForm

useForm is the base hook containing all @per-form/react logic.

Parameters

useForm take only one parameter that is an object with the following shape:

PropertyTypeDefaultDescription
defaultValuesRecord<string, unknown>Form default values (see default values guide for more information)
filterLocalErrorsbooleantrueFilter local errors from the global form errors (see local fields guide for more information)
focusOnErrorbooleantrueFocus the first field error or not
formHTMLFormElementnullForm ref
messagesRecord<string, string>Custom messages (see messages guide for more information)
mode'all' | 'blur' | 'change' | 'submit''submit'Validation strategy (see mode guide for more information)
onBlurOptOutstring[] | stringOpt-out for native onBlur event
onChangeOptOutstring[] | stringOpt-out for native onChange event (see controlled components guide for more information)
onResetIResetHandlerReset callback
onSubmitISubmitHandlerSubmit callback
onSubmitErrorISubmitErrorHandlerSubmit error callback (only when useNativeValidation=false)
revalidateMode'blur' | 'change' | 'submit''submit'Re-validation strategy (see mode guide for more information)
transformersRecord<string, (value: unknown) => unknown>Transformers for type casting see type casting guide for more information)
useNativeValidationbooleanUse native browser validation or use error state
validatorsRecord<string, IValidator | IValidatorObject>Custom field validators (see validation guide for more information)

See here for the types.

Returns

useForm return an object with the following shape:

PropertyTypeDescription
errorsIErrorForm errors (see errors guide for more information)
formRefObject<HTMLFormElement>Form ref
formPropsIFormPropsForm props to add on your form component
messagesRecord<string, string>Given custom messages
mode'all' | 'blur' | 'change' | 'submit'Chosen Validation strategy
onChangeIOnChangeHandlerChange handler (see type casting guide and controlled components guide for more information)
onErrorIOnErrorHandlerManual error handler (see controlled components guide for more information)
onResetIOnResetHandlerReset handler (see reset guide for more information)
onSubmitIOnSubmitHandlerSubmit handler (see submit guide for more information)
registerIRegisterInternal usage
resetIFormResetManual reset function
revalidateMode'blur' | 'change' | 'submit'Chosen re-validation strategy
statesIFormStatesForm states (warning: these are not react states)
subscribeISubscribeFunction to subscribe to state change (see the form state guide for more information)
unregisterIUnregisterInternal usage
useNativeValidationbooleanChoice for native validation or not
validateIFormValidateManual validate function
watchIWatchManual watch function (see watch guide for more information)

See here for the types.

Usage

Just add {...formProps} to your <form> and you are ready:

0
import type { FormEvent } from 'react';
import type { IProps } from '../types';
import { type IFormValues, useForm } from '@per-form/react';

export default function Demo(props: IProps) {
function handleSubmit(_e: FormEvent<HTMLFormElement>, values: IFormValues) {
console.log(values);
}

const { errors, formProps } = useForm({
...props,
onSubmit: handleSubmit,
});

return (
<form {...formProps}>
<input name="text" required />
{errors.all.text && <div className="error">{errors.all.text}</div>}
<button type="submit">Submit</button>
</form>
);
}