| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package form
- import (
- "bytes"
- "context"
- "embed"
- "html/template"
- "strconv"
- )
- /* ValidatorFunc defines a function for FormField data validation */
- type ValidatorFunc func(*FormField, context.Context) error
- /* ValidatorsList defines a list of ValidatorFunc */
- type ValidatorsList []ValidatorFunc
- /* A general purpose form field struct */
- type FormField struct {
- Name string
- Error []error
- Value string
- Label string
- Class string
- Type string
- Placeholder string
- Help string
- Required bool
- AutoFocus bool
- Sticky bool
- Validators ValidatorsList
- }
- /* SetValidators configures validators list in form field */
- func (f *FormField) SetValidators(validators ValidatorsList) *FormField {
- f.Validators = validators
- return f
- }
- /* SetLabel configures form label */
- func (f *FormField) SetLabel(label string) *FormField {
- f.Label = label
- return f
- }
- /* SetClass configures class name in form field */
- func (f *FormField) SetClass(class string) *FormField {
- f.Class = class
- 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() (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 */
- func (f *FormField) Int() int {
- if result, err := strconv.Atoi(f.Value); err == nil {
- return result
- }
- return 0
- }
- /* GetFloat returns FormField.Value as float */
- func (f *FormField) GetFloat() (float64, error) {
- return strconv.ParseFloat(f.Value, 64)
- }
- /* Float converts FormField.Value to float and ignores errors */
- func (f *FormField) Float() float64 {
- if result, err := strconv.ParseFloat(f.Value, 64); err == nil {
- return result
- }
- return 0.0
- }
- /* GetBool returns boolean value for checkbox fields */
- func (f *FormField) GetBool() (bool, error) {
- /* placeholder */
- return false, nil
- }
- /* GetChecked returns true if checkbox has been checked and its value is "on" */
- func (f *FormField) GetChecked() bool {
- return f.Value == "on"
- }
- /* formFieldTemplate is a template to render FormField element in HTML format */
- //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 */
- var formTemplate = template.Must(
- template.New("FormField").Parse(
- string(must(formTemplates.ReadFile("templates/field.html")).([]byte)),
- ),
- )
- /* 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("")
- }
|