go-form-demo/form/theme.go

39 lines
993 B
Go

package form
import (
"net/http"
"gitnet.fr/deblan/go-form/form"
"gitnet.fr/deblan/go-form/validation"
)
type Theme struct {
Value string `field:"lowerCamel"`
}
func NewTheme() *Theme {
return &Theme{Value: "Html5"}
}
func CreateThemeSelectorForm() *form.Form {
choices := form.NewChoices([]string{"Html5", "Bootstrap5"})
choices.LabelBuilder = func(key int, item any) string { return item.(string) }
choices.ValueBuilder = func(key int, item any) string { return item.(string) }
return form.NewForm(
form.NewFieldChoice("value").
WithOptions(
form.NewOption("choices", choices),
form.NewOption("label", "Select a theme"),
form.NewOption("required", true),
).
WithConstraints(validation.NewNotBlank()),
form.NewSubmit("").
WithData("Change").
WithOptions(form.NewOption("attr", form.Attrs{"class": "btn-primary"})),
).
End().
WithName("").
WithOptions(form.NewOption("attr", form.Attrs{"id": "theme-form"})).
WithMethod(http.MethodGet)
}