useForm
Hook that provides a high-level interface to work with Forms, and more particularly, with Form validations. It incorporates all the good practices to provide a great User Experience for your Forms.
Signature
function useForm<T extends Form.Values>(props: {
onSubmit: (values: T) => void | boolean | Promise<void | boolean>;
initialValues?: Partial<T>;
validation?: {
[id in keyof T]?: ((value: T[id]) => string | undefined | null) | FormValidation;
};
}): {
handleSubmit: (values: T) => void | boolean | Promise<void | boolean>;
itemProps: {
[id in keyof T]: Partial<Form.ItemProps<T[id]>> & {
id: string;
};
};
setValidationError: (id: keyof T, error: ValidationError) => void;
setValue: <K extends keyof T>(id: K, value: T[K]) => void;
values: T;
focus: (id: keyof T) => void;
reset: (initialValues?: Partial<T>) => void;
};Arguments
onSubmitis a callback that will be called when the form is submitted and all validations pass.
With a few options:
initialValuesare the initial values to set when the Form is first rendered.validationare the validation rules for the Form. A validation for a Form item is a function that takes the current value of the item as an argument and must return a string when the validation is failing. We also provide some shorthands for common cases, see FormValidation.
Return
Returns an object which contains the necessary methods and props to provide a good User Experience in your Form.
handleSubmitis a function to pass to theonSubmitprop of the<Action.SubmitForm>element. It wraps the initialonSubmitargument with some goodies related to the validation.itemPropsare the props that must be passed to the<Form.Item>elements to handle the validations.
It also contains some additions for easy manipulation of the Form's data.
valuesis the current values of the Form.setValueis a function that can be used to programmatically set the value of a specific field.setValidationErroris a function that can be used to programmatically set the validation of a specific field.focusis a function that can be used to programmatically focus a specific field.resetis a function that can be used to reset the values of the Form. Optionally, you can specify the values to set when the Form is reset.
Example
Types
FormValidation
Shorthands for common validation cases
Enumeration members
Required
Show an error when the value of the item is empty
Last updated

