UseForm hook
UseForm is a specialized custom hook designed for advanced form management, serving as the foundational core for the Form component. This hook comes in handy when you require greater control over your forms, offering access to essential objects such as values and errors. It's particularly valuable when you opt for alternative Field components that differ from FormFusion's default Input and Textarea components. With UseForm, you can tailor your form management to your specific needs, ensuring a flexible and adaptable solution for your web development projects.
Available parameters
#
|
#
|
#
|
#
|
Available configuration
The UseForm hook yields the entire form configuration, which you can utilize to tailor your form management precisely as desired. Here's an example of how to implement it:
const config = useForm({onSubmit: (values) => {console.log(values)}});
In the provided code snippet, the config
variable includes the following essential configuration parameters:
#
|
#
|
#
|
#
|
#
|
Example
import { Form, Input, useForm } from 'formfusion';
import './App.css';
const MyForm = () => {
const onSubmit = (data: object) => {
console.log('Success ' + JSON.stringify(data));
};
const config = useForm({ onSubmit });
return (
<Form config={config} className="form">
<Input
id="username"
name="username"
type="username"
label="Username"
required
classes={{
field: 'input-field',
label: 'input-field__label',
error: 'input-field__error-message',
}}
validation={{
patternMismatch: 'Please match the requested format.',
valueMissing: 'This field is required.',
}}
/>
Your username is {config.values.username}
<button type="submit">Submit</button>
</Form>
);
};
export default MyForm;