Auto Form
Automatically generate form fields based on a schema.
Support Fields
- Color Picker
- Input
- File Input
- Textarea
- Checkbox
- Select
- Radio Group
- Switch
- Text Language Input
- Icon Picker
- Tag Input
- Combobox
- Date Picker
- WYSIWYG Editor
Installation
Usage
Component Props
Zod Configuration
Validators
Our component is smart and well integrated with Zod, so you can use all the validators that Zod provides. For exampele if you use zod.string().min(8)
, the input will automatically have a minlength="8" attribute.
Validation methods not supported by HTML will automatically be checked when the form is submitted.
Descriptions
You can add a description to your field by passing the description
prop.
Optional Fields
By default all fields are required. You can make a field optional by using the optional
method.
Default Values
You can set a default value for a field by using the default
method.
Label
You can set a label for a field by using the label
method.
Submit Form
You can use the onSubmit
prop to get the form data.
You can get access to the form
from react-hook-form
by using the form
prop as second argument.
You can also move onSubmit
to a separate function.
Handle Errors
You can handle errors by using the Sonner component.
Submit Button
Component has a submit button by default with the text Save
include translation for this text. You can change the component by using the submitButton
prop.
Remember to pass the props
to the button to keep the submit functionality.
Theme
You can change the theme of the form by using the theme
prop and passing horizontal
or vertical
.
Get Values from Form
You can get the values from the form by using the onValuesChange
prop.
Dependencies
AutoForm allow to create dependencies between fields. For example allowing one field's visibility or behavior to be controlled based on the value of another. Pass dependencies
prop with an array of objects.
Hides
For example if provider
is not smtp
, the field smtp
will be hidden.
Requires
For example if provider
is resend
, the field resend_key
will be required.
This dependencie will hide only the Optional
text. That's mean you should change your formSchema
to make the field required.
For example using refine
method from Zod
.
Disables
For example if provider
is not smtp
, the field smtp
will be disabled.
Set options
For example if vegetarian
checkbox hides the Beef Wellington
option from mealOptions
if its not already selected.