|
@@ -3,6 +3,7 @@ package form
|
|
|
import (
|
|
import (
|
|
|
"bytes"
|
|
"bytes"
|
|
|
"context"
|
|
"context"
|
|
|
|
|
+ "embed"
|
|
|
"html/template"
|
|
"html/template"
|
|
|
"strconv"
|
|
"strconv"
|
|
|
)
|
|
)
|
|
@@ -25,6 +26,7 @@ type FormField struct {
|
|
|
Help string
|
|
Help string
|
|
|
Required bool
|
|
Required bool
|
|
|
AutoFocus bool
|
|
AutoFocus bool
|
|
|
|
|
+ Sticky bool
|
|
|
Validators ValidatorsList
|
|
Validators ValidatorsList
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -123,21 +125,23 @@ func (f *FormField) GetChecked() bool {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* formFieldTemplate is a template to render FormField element in HTML format */
|
|
/* formFieldTemplate is a template to render FormField element in HTML format */
|
|
|
-const formFieldTemplate = `
|
|
|
|
|
- {{ if .Label }}<label class="form-label" for="{{ .Name }}">{{ .Label }}</label>{{ end }}
|
|
|
|
|
- <input type="{{ .Type }}" id="{{ .Name }}" name="{{ .Name }}"
|
|
|
|
|
- {{- if .Class }} class="{{ .Class }}"{{ end }}
|
|
|
|
|
- {{- if and .Value (ne .Type "password") }} value="{{ .Value }}"{{ end }}
|
|
|
|
|
- {{- if .Placeholder}} placeholder="{{ .Placeholder }}"{{ end }}
|
|
|
|
|
- {{- if .Help }} aria-describedby="{{ .Name }}Help"{{ end }}
|
|
|
|
|
- {{- if .Required }} required{{ end }}
|
|
|
|
|
- {{- if .AutoFocus }} autofocus{{ end }}>
|
|
|
|
|
- {{ if .Help }}<div id="{{ .Name }}Help" class="form-text">{{ .Help }}</div>{{ end }}
|
|
|
|
|
- {{ if .Error }}{{ range $e := .Error }}<div class="text-danger">{{ $e }}</div>{{ end }}{{ end }}
|
|
|
|
|
-`
|
|
|
|
|
|
|
+//go:embed templates/*
|
|
|
|
|
+var formTemplates embed.FS
|
|
|
|
|
+
|
|
|
|
|
+/* must checks for compile/start up errors */
|
|
|
|
|
+func must(r any, e error) any {
|
|
|
|
|
+ if e != nil {
|
|
|
|
|
+ panic(e)
|
|
|
|
|
+ }
|
|
|
|
|
+ return r
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
/* formTemplate is compiled template to render FormField element in HTML format */
|
|
/* formTemplate is compiled template to render FormField element in HTML format */
|
|
|
-var formTemplate = template.Must(template.New("FormField").Parse(formFieldTemplate))
|
|
|
|
|
|
|
+var formTemplate = template.Must(
|
|
|
|
|
+ template.New("FormField").Parse(
|
|
|
|
|
+ string(must(formTemplates.ReadFile("templates/field.html")).([]byte)),
|
|
|
|
|
+ ),
|
|
|
|
|
+)
|
|
|
|
|
|
|
|
/* HTML renders FormField element in html format */
|
|
/* HTML renders FormField element in html format */
|
|
|
func (f *FormField) HTML() template.HTML {
|
|
func (f *FormField) HTML() template.HTML {
|