Methods
Methods
Here’s the complete documentation with examples for each method:
changeValue
Updates the value of a specific field in the form.
const { changeValue } = useForm();
changeValue('username', 'newUsername');
errors
An object containing validation errors for the form fields.
const { errors } = useForm();
console.log(errors);
field
Options and configurations for the form fields.
const { field } = useForm();
console.log(field);
form
Representation of the current state of the form data.
const { form } = useForm();
console.log(form);
getErrors
Get the validation errors for a specific field in the form.
const { getErrors } = useForm();
const errors = getErrors('username');
getIsDirtyField
Checks if a specific field in the form has been modified.
const { getIsDirtyField } = useForm();
const isUsernameDirty = getIsDirtyField('username');
getValue
Get the current value of a specific field in the form.
const { getValue } = useForm();
const usernameValue = getValue('username');
handleSubmit
Handles form submission.
const { handleSubmit } = useForm();
const onSubmit = handleSubmit((form) => {
// Your form submission logic here
});
hasError
Checks if a specific field in the form has validation errors.
const { hasError } = useForm();
const hasErrorUsername = hasError('username');
isDirty
Indicates whether the form has been modified.
const { isDirty } = useForm();
console.log(isDirty.value);
isValid
Indicates whether the form is currently in a valid state.
const { isValid } = useForm();
console.log(isValid);
onChange
Attaches an event listener to a specific form field.
const { onChange } = useForm();
const handleChange = onChange('username');
reset
Resets the form to a specified state.
const { reset } = useForm();
reset({ username: '', password: '' });
resetIsDirty
Resets the dirty state of the form.
const { resetIsDirty } = useForm();
resetIsDirty();
setError
Sets a validation error for the form or a specific field.
const { setError } = useForm();
setError({ error: 'Invalid username', path: 'username' });
watch
Registers a callback function to watch for changes in the form’s state.
const { watch } = useForm();
watch((changedKeys) => {
console.log('Changed keys:', changedKeys);
});