|
@@ -1,7 +1,9 @@
|
|
|
package form
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"context"
|
|
|
+ "html/template"
|
|
|
"strconv"
|
|
|
)
|
|
|
|
|
@@ -13,12 +15,17 @@ type ValidatorsList []ValidatorFunc
|
|
|
|
|
|
/* A general purpose form field struct */
|
|
|
type FormField struct {
|
|
|
- Name string
|
|
|
- Label string
|
|
|
- Value string
|
|
|
- Class string
|
|
|
- Error error
|
|
|
- Validators *ValidatorsList
|
|
|
+ Name string
|
|
|
+ Error []error
|
|
|
+ Value string
|
|
|
+ Label string
|
|
|
+ Class string
|
|
|
+ Type string
|
|
|
+ Placeholder string
|
|
|
+ Help string
|
|
|
+ Required bool
|
|
|
+ AutoFocus bool
|
|
|
+ Validators *ValidatorsList
|
|
|
}
|
|
|
|
|
|
/* SetValidators configures validators list in form field */
|
|
@@ -39,14 +46,48 @@ func (f FormField) SetClass(class string) FormField {
|
|
|
return f
|
|
|
}
|
|
|
|
|
|
+/* SetRequired marks FormField as mandatory */
|
|
|
+func (f FormField) SetRequired() FormField {
|
|
|
+ f.Required = true
|
|
|
+ return f
|
|
|
+}
|
|
|
+
|
|
|
+/* SetAutoFocus gives focus to current FormField on page load */
|
|
|
+func (f FormField) SetAutoFocus() FormField {
|
|
|
+ f.AutoFocus = true
|
|
|
+ return f
|
|
|
+}
|
|
|
+
|
|
|
+/* SetType specifies input field type */
|
|
|
+func (f FormField) SetType(t string) FormField {
|
|
|
+ f.Type = t
|
|
|
+ return f
|
|
|
+}
|
|
|
+
|
|
|
+/* SetPlaceholder specified a placeholder property in Formfield */
|
|
|
+func (f FormField) SetPlaceholder(placeholder string) FormField {
|
|
|
+ f.Placeholder = placeholder
|
|
|
+ return f
|
|
|
+}
|
|
|
+
|
|
|
+/* SetHelp specified a help message for current Formfield */
|
|
|
+func (f FormField) SetHelp(help string) FormField {
|
|
|
+ f.Help = help
|
|
|
+ return f
|
|
|
+}
|
|
|
+
|
|
|
/* GetString returns FormField.Value as string */
|
|
|
func (f FormField) GetString() string {
|
|
|
return f.Value
|
|
|
}
|
|
|
|
|
|
/* GetInt returns FormField.Value as int */
|
|
|
-func (f FormField) GetInt() (int, error) {
|
|
|
- return strconv.Atoi(f.Value)
|
|
|
+func (f FormField) GetInt() (v int, err error) {
|
|
|
+ v, err = strconv.Atoi(f.Value)
|
|
|
+ if err != nil {
|
|
|
+ err = EInvalidIntValue
|
|
|
+ }
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
/* Int converts FormField.Value to integer value and ignores errors */
|
|
@@ -80,3 +121,29 @@ func (f FormField) GetBool() (bool, error) {
|
|
|
func (f FormField) GetChecked() bool {
|
|
|
return f.Value == "on"
|
|
|
}
|
|
|
+
|
|
|
+/* 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 .Value }} 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 }}
|
|
|
+`
|
|
|
+
|
|
|
+/* formTemplate is compiled template to render FormField element in HTML format */
|
|
|
+var formTemplate = template.Must(template.New("FormField").Parse(formFieldTemplate))
|
|
|
+
|
|
|
+/* HTML renders FormField element in html format */
|
|
|
+func (f FormField) HTML() template.HTML {
|
|
|
+ var buffer bytes.Buffer
|
|
|
+ if err := formTemplate.Execute(&buffer, f); err == nil {
|
|
|
+ return template.HTML(buffer.String())
|
|
|
+ }
|
|
|
+ return template.HTML("")
|
|
|
+}
|