Form Data Sources
Iridium can interpret your structs using reflection, but it still needs to understand where to fetch and commit data to. That is where drivers come into play.
Form support four drivers:
- GORM <- our interface over GORM
- Array <- our in-memory array driver
- Nil <- empty driver with no hooks
- Custom <- custom drivers you write yourself
We'll examine the GORM driver here, since most users will likely be using that to drive their forms. (See drivers for more info on array & custom drivers to get more out of your forms)
Gorm Driver
When working with a GORM struct, you can provide your form a GORM driver to have it automatically fetch and commit values directly to your form.
Example:
//user.go
type User struct {
gorm.Model
Name string
Email string
Active bool
}
//user_form.go
gormDriver := drivers.NewGormDriver[models.User](db)
form.NewForm[models.User]().
Driver(gormDriver)
Schema(
FormInput("name"),
FormInput("email"),
FormSwitch("active"),
)In this example, you've instantiated a new GORM driver based off your models.User struct, passed in your *gorm.DB pointer called db, and then passed that driver to your form definition.
Now Iridium will automatically read and write to your GORM model, and hence, your database table.
On your DB pointer
Iridium does not own or maintain a GORM DB pointer for you. It's your responsibilty to properly migrate your GORM structs and maintain your own database connection with your own database driver.
So you'll have to write some basic setup for GORM for your application and provide this DB pointer to Iridium when required. Please refer to the GORM docs, and/or see our demo application for an example of how to handle this.