useForm
Gather data from user and send it to a server. Send it as-is or after some (client-side) validation and/or transformation.
In React, a form uses either controlled or uncontrolled inputs. Mantine provides both options.
The Mantine's useForm hook attempts to provide a convenient way to specify initial data, and specify validation and/or transformation to perform on submit.
Overview
We work with a regular <form>, a set of inputs, and a submit button.
We provide initial data in an object (initialValues). The user may edit data through the inputs.
On submit, Mantine collects the data and provide it to the validator and/or transformer. Then, it provides the processed data to the callback, in principle for it to send it to the server.
When an initial value has no matching input (invisible, uneditable property), Mantine still collects it.
main steps
- we create a configuration object
- We compute a configured form object, through useForm(configuration)
- We generate the form-submit handler with form.onSubmit(f), where f is our callback to handle the processed data. The form-submit handler takes an event as argument, and is assigned to the <form> onSubmit attribute, which is the react version of standard onsubmit.
- We add attributes to the form inputs so that they interoperate. we spread form.getInputProps(email) to add the relevant attributes on the input, depending if it is controlled or uncontrolled: value if it is controlled, initialValue and ref if it is uncontrolled. They also get onChange and onBlur.
init form
const form = useForm({ // config
initialValues: {x: "", y: ""},
validate: f
transformValues: (collected) => processed
})
form callback
function my_callback(data) {
data.x
data.y
}
const mantineEventHandler = form.onSubmit(my_callback)
wire up
form onSubmit={e => mantineEventHandler(e)}
TextInput label="x" {...form.getInputProps('x')}
TextInput label="x" {...form.getInputProps('y')}
TextInput label="x" {...form.getInputProps('z')} // z is not part of initial values, but may still be included if input is changed}
/form
initial value or not.
Mantine collect properties from the initial values, even if there is no matching input, and even if the value is null or undefined, and provide them for processing.
If an input is connected to an initial value that does not exist, it is effectively initialized to undefined, which will generate a React warning. Mantine will collect the value of that input and associate with the key provided in form.getInputProps??
initial value for a TextInput
The initial value for a TextInput should no be null: it triggers a React warning. It should also not be set to undefined. Instead, we should initialize to empty string. We may do post-processing in the transformer to change the empty string to null.
utils
// get current form values
form.values
// Set all or some form values
form.setValues(values)
// Set all form values using the previous state
form.setValues((prev) => ({ ...prev, ...values }))
// Set value of single field
form.setFieldValue("path", value)
// Set value of nested field
form.setFieldValue("user.firstName", "Jane")
// Resets form.values to initialValues,
// clears all validation errors,
// resets touched and dirty state
form.reset()
// Sets initial values, used when form is reset
form.setInitialValues({ values: "object" })