form

package module
v0.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 16 Imported by: 0

README

go-form

A Go library for rendering HTML forms from Go structs using struct tags and Go templates. Supports multiple template styles (Plain, Bootstrap 5, Tailwind CSS) and a wide range of HTML input types.


Features

  • Define forms as Go structs with struct tags for field type, label, placeholder, and more
  • Supports many HTML input types: text, password, email, tel, number, date, color, range, datetime-local, time, week, month, hidden
  • Checkbox, radio, dropdown, and textarea fields
  • Grouping and nested struct support for form sections
  • Built-in template sets: Plain, Bootstrap 5, Tailwind CSS
  • Integrates with html/template via a FuncMap
  • CSRF Protection
  • SortedSelect and SortedMultiSelect for type-safe, mapped dropdowns and multi-selects (see examples)

Installation

go get github.com/donseba/go-form

Quick Start

import (
    "github.com/donseba/go-form"
    "github.com/donseba/go-form/templates"
    "html/template"
)

type ExampleForm struct {
    Username string `form:"input,text" label:"Username" placeholder:"Enter your username" required:"true"`
    Password string `form:"input,password" label:"Password" placeholder:"Enter your password" required:"true"`
    Email    string `form:"input,email" label:"Email" placeholder:"Enter your email" required:"true"`
    Age      int    `form:"input,number" label:"Age" placeholder:"Enter your age" step:"1"`
}

f := form.NewForm(templates.Plain) // or templates.BootstrapV5, templates.TailwindV3
funcMap := f.FuncMap()
tmpl := template.Must(template.New("form").Funcs(funcMap).Parse(`{{ form_render .Form nil }}`))

Supported Templates

Template Name Description
templates.Plain Plain HTML, minimal styles
templates.BootstrapV5 Bootstrap 5 form styles
templates.TailwindV3 Tailwind CSS v3 styles

Supported Input Fields & Options

Field Type / Tag Example Description Options (Struct Tags)
form:"input,text" Text input label, placeholder, required, maxlength
form:"input,password" Password input label, placeholder, required
form:"input,email" Email input label, placeholder, required
form:"input,number" Number input label, placeholder, required, min, max, step
form:"input,date" Date input label, placeholder, required
form:"input,datetime-local" DateTime input label, placeholder, required
form:"input,time" Time input label, placeholder, required
form:"input,week" Week input label, placeholder, required
form:"input,month" Month input label, placeholder, required
form:"input,color" Color input label, placeholder, required
form:"input,range" Range input label, min, max, step
form:"input,hidden" Hidden input value
form:"input,search" Search input label, placeholder
form:"input,url" URL input label, placeholder
form:"input,tel" Telephone input label, placeholder
form:"input,image" Image input label, src, alt
form:"checkbox" Checkbox label, required
form:"radios" Radio group label, values (e.g. a:A;b:B), required
form:"dropdown" Dropdown/select label, values (e.g. a:A;b:B), required
form:"textarea" Multi-line text label, placeholder, rows, cols, maxlength

Other supported tags:

  • legend — For grouping/nested structs (section title)
  • description — Field description/help text
  • maxLength — Maximum length for textarea or string input
  • class — Custom CSS class for the field
  • data — Custom data attributes (e.g., data="custom:value,foo:bar,baz:qux")
  • translate — Enable translation for enum values (e.g., translate:"true" for Enumerator fields)

Validation

Built-in Validation
  • required: Ensures the field is not empty.
  • min, max, step: For numeric fields, enforces minimum, maximum, and step values.
  • minLength, maxLength: For string/textarea fields, enforces minimum and maximum character count (Unicode-aware).
  • values: For radios/dropdowns, ensures the value is one of the allowed options.
  • Email format: Checks for a valid email address format (basic @ check).
  • Enumerator, Mapper, SortedMapper: If a field implements one of these interfaces, the value must be present in the allowed set returned by Enum(), Mapper(), or SortedMapper().
Using Enumerator, Mapper, and SortedMapper Interfaces

For enum values, implement Enumerator:

type Status string
func (s Status) Enum() []any { return []any{"active", "inactive"} }

type MyForm struct {
    Status Status `form:"dropdown" label:"Status"`
}

For key-value pairs, use Mapper (unordered) or SortedMapper (ordered):

type ColorMap string
func (c ColorMap) Mapper() map[string]string {
    return map[string]string{"red": "Red", "blue": "Blue"}
}

// For ordered pairs, implement SortedMapper with []SortedMap
Custom Validation

You can add your own validation logic using the validate struct tag and by registering a custom validation function:

// 1. Define your validation function (must return form.FieldErrors)
func isHexColor(val any, field reflect.StructField) form.FieldErrors { /* ... */ }

// 2. Register it with your Form instance
f.RegisterValidationMethod("isHexColor", isHexColor)

// 3. Use it in your struct
type MyForm struct {
    Color string `form:"input,text" label:"Color" validate:"isHexColor"`
}

// 4. Call f.ValidateForm(&myForm) to run both built-in and custom validations

Custom validators can be chained with commas in the validate tag. All errors are collected and can be rendered in your template.


Translation / Internationalization

go-form supports translation of form labels, error messages, and other UI text. You can provide your own translation function and a Localizer implementation to render forms in different languages or customize the wording for your application.

How to Use
  1. Create a translation function: This function receives a Localizer, a key, and optional arguments, and returns the translated string.
  2. Implement a Localizer: This determines the current locale (e.g., from the user session or request).
  3. Create the form with translation support: Use form.NewTranslatedForm(template, translateFunc).
  4. Pass your Localizer when rendering or validating: The form will use your translation function and Localizer to fetch translations.
// Example translation function and Localizer
var translations = map[string]map[string]string{
    "en": {"Name": "Name", "form.validation.required": "is required"},
    "it": {"Name": "Nome", "form.validation.required": "è obbligatorio"},
}

type MyLocalizer struct { Locale string }
func (l MyLocalizer) GetLocale() string { return l.Locale }

func myTranslate(loc form.Localizer, key string, args ...any) string {
    locale := "en"
    if l, ok := loc.(MyLocalizer); ok {
        locale = l.Locale
    }
    msg := key
    if m, ok := translations[locale]; ok {
        if t, ok := m[key]; ok {
            msg = t
        }
    }
    if len(args) > 0 {
        return fmt.Sprintf(msg, args...)
    }
    return msg
}

f := form.NewTranslatedForm(templates.Plain, myTranslate)
// When rendering or validating, pass your Localizer:
loc := MyLocalizer{Locale: "it"}
// ...

See the example in example/translation/main.go for a complete usage demonstration.

Translating Enum Values

By default, enum values display as-is. To enable translation, add translate:"true":

type Status string
func (s Status) Enum() []any { return []any{"active", "inactive"} }

type MyForm struct {
    Status Status `form:"dropdown" label:"Status" translate:"true"`
}

To enable translation for all enums by default, set the global variable:

import "github.com/donseba/go-form"

func init() {
    form.DefaultEnumTranslation = true  // All enums will be translatable by default
}

Individual fields can still opt-out using translate:"false". Translation keys follow the format enum||{TypeName}.{value}:

var translations = map[string]map[string]string{
    "en": {"enum||Status.active": "Active", "enum||Status.inactive": "Inactive"},
    "it": {"enum||Status.active": "Attivo", "enum||Status.inactive": "Inattivo"},
}

Custom Form Attributes

You can set custom HTML attributes on forms (e.g., hx-post, data-*, etc.) using the Attributes field in your form struct:

form := &FormField{
    // ...other fields...
    Attributes: map[string]string{
        "hx-post": "/some-url",
        "data-custom": "value",
    },
}
Input Groups (Prepend/Append)

You can prepend or append content to input fields using the group tag. This is supported in all template sets (Plain, Bootstrap 5, Tailwind CSS):

type ExampleForm struct {
    Username string `form:"input,text" label:"Username" group:"@,.com"`
}

This will render an input with @ before and .com after the field, styled according to the selected template.


CSRF Protection

go-form includes built-in CSRF (Cross-Site Request Forgery) protection for your forms. This prevents attackers from tricking users into submitting unauthorized requests.

Basic Usage
  1. Create a form renderer which adds a default CSRF protection by default:

    formRenderer := form.NewForm(templates.BootstrapV5)
    
  2. Apply the CSRF middleware to your handlers:

    // With standard http.ServeMux:
    protectedHandler := formRenderer.CSRFMiddleware()(yourHandler) // <-- wrap your handler
    mux.Handle("/", protectedHandler)
    
    // With Chi router:
    import "github.com/go-chi/chi/v5"
    
    router := chi.NewRouter()
    router.Use(formRenderer.CSRFMiddleware()) // <-- load the middleware
    
  3. Inject the CSRF token into your form object Info before rendering:


  loginForm := LoginForm{
    Info: form.Info{
      Target:     "/login",
      Method:     "post",
      SubmitText: "Log In",
    },
  }

  form.InjectCSRFToken(r, &loginForm.Info)

The middleware automatically:

  • Generates a secure random token for each form
  • Validates the token on submission
  • Refreshes tokens after each submission
  • Rejects requests with missing or invalid tokens
Custom Error Handling

By default, CSRF validation failures return HTTP error responses. For a better user experience, you can provide custom error handling:

options := form.CSRFOptions{
  ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
    switch {
      case errors.Is(err, csrf.ErrTokenMismatch):
      http.Error(w, "Invalid CSRF token", http.StatusForbidden)
      case errors.Is(err, csrf.ErrTokenExpired):
      http.Error(w, "CSRF token expired", http.StatusForbidden)
      case errors.Is(err, csrf.ErrKeyOrTokenEmpty):
      http.Error(w, "CSRF token or session ID is empty", http.StatusBadRequest)
      case errors.Is(err, csrf.ErrTokenNotFound):
      http.Error(w, "CSRF token not found", http.StatusBadRequest)
      default:
      http.Error(w, "CSRF validation error: "+err.Error(), http.StatusBadRequest)
    }
  },
}

// Use the custom options
protectedHandler := formRenderer.CSRFMiddlewareWithOptions(options)(yourHandler)
Alternative CSRF Stores

The default in-memory CSRF store is suitable for single-server applications. For production or distributed environments, you can implement a custom CSRFStore that uses Redis, a database, or another shared storage mechanism:

// Example Redis CSRF Store implementation
type RedisCSRFStore struct {
    client *redis.Client
    prefix string
    ttl    time.Duration
}

func (s *RedisCSRFStore) Store(key, token string) error {
    return s.client.Set(ctx, s.prefix+key, token, s.ttl).Err()
}

func (s *RedisCSRFStore) Get(key string) (string, error) {
    val, err := s.client.Get(ctx, s.prefix+key).Result()
    if err == redis.Nil {
        return "", csrf.ErrTokenNotFound
    }
    return val, err
}

// ... implement other required methods ...

// Then use it with your form:
store := &RedisCSRFStore{
    client: redisClient,
    prefix: "csrf:",
    ttl:    30 * time.Minute,
}
formRenderer.SetCSRFStore(store)

See the example in example/csrf/main.go for a complete usage demonstration.


SortedSelect and SortedMultiSelect

SortedSelect and SortedMultiSelect are generic types for type-safe, mapped dropdowns and multi-selects with custom key types. They support form mapping, validation, database integration, and JSON serialization.

  • SortedSelect is for single-value dropdowns (e.g., DepartmentID form.SortedSelect[int64]).
  • SortedMultiSelect is for multi-value selections (e.g., DepartmentsMulti form.SortedMultiSelect[int64]).
  • Both support any comparable Go type as the key: int, int64, string, float64, uuid.UUID, time.Time, etc.
  • They work seamlessly with form rendering, validation, and database/sql or JSON marshalling.

Minimal usage example:

import "github.com/donseba/go-form"

// Single select
DepartmentID form.SortedSelect[int64] `form:"dropdown" label:"Department"`

// Multi select
DepartmentsMulti form.SortedMultiSelect[int64] `form:"multicheckbox" label:"Departments"`

// Initialize with a source map
form.NewSortedSelect(map[int64]string{1: "HR", 2: "IT"})
form.NewSortedMultiSelect(map[int64]string{1: "HR", 2: "IT"})

For advanced usage, see example/sortedselect/main.go — covers single and multi-select fields, supported key types, pre-filled and user-submitted values, validation, error handling, JSON and DB integration.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMapFormNotPointer = &MapFormError{"dst must be a pointer to struct"}
	ErrMapFormNotStruct  = &MapFormError{"dst must be a pointer to struct"}
)
View Source
var (
	DefaultSubmitText      = "Submit"
	DefaultEnumTranslation = false // Set to true to enable translation for all enums by default
)
View Source
var (
	TranslationKeyRequired                      = "form||Validation required"
	TranslationKeyMin                           = "form||Value should be greater than or equal to %v"
	TranslationKeyMax                           = "form||Value should be less than or equal to %v"
	TranslationKeyMaxLength                     = "form||Value should not exceed %d characters"
	TranslationKeyMinLength                     = "form||Value should be at least %d characters"
	TranslationKeyInvalidValue                  = "form||Invalid value '%s' provided"
	TranslationKeyInvalidEmail                  = "form||Invalid email format"
	TranslationKeyInvalidEnum                   = "form||Invalid enum value '%s' provided"
	TranslationKeyInvalidMapper                 = "form||Invalid mapper value '%s' provided"
	TranslationKeyInvalidSortedMapper           = "form||Invalid sorted mapper value '%s' provided"
	TranslationKeyPattern                       = "form||Value does not match the required pattern '%s'"
	TranslationKeyURL                           = "form||Invalid URL format"
	TranslationKeyBool                          = "form||Value should be either true or false"
	TranslationKeyZero                          = "form||Value should be zero"
	TranslationKeyMinItems                      = "form||Value should have at least %d items"
	TranslationKeyMaxItems                      = "form||Value should have at most %d items"
	TranslationKeyPrefix                        = "form||Value should start with '%s'"
	TranslationKeySuffix                        = "form||Value should end with '%s'"
	TranslationKeyContains                      = "form||Value should contain '%s'"
	TranslationKeyStep                          = "form||Value should be a multiple of %f"
	TranslationKeyCSRFTokenMissing              = "form||CSRF token is missing"
	TranslationKeyCSRFTokenInvalid              = "form||Invalid CSRF token"
	TranslationKeyCSRFTokenError                = "form||Error processing CSRF token"
	TranslationKeySortedSelectNotFound          = "form||SortedSelect: value '%v' not found in source"
	TranslationKeySortedSelectKeyNotFound       = "form||SortedSelect: key '%s' not found in source"
	TranslationKeySortedSelectSourceNil         = "form||SortedSelect: source map is nil"
	TranslationKeySortedSelectTypeError         = "form||Cannot assign or convert value of type %T to %s"
	TranslationKeySortedSelectUnmarshalNotFound = "form||SortedSelect: value '%v' not found in source during unmarshal"
)
View Source
var (
	DefaultCSRFField = "_csrf"
)

CSRF errors

Functions

func GetCSRFToken added in v0.12.0

func GetCSRFToken(r *http.Request) (string, bool)

GetCSRFToken retrieves the CSRF token from the request context

func InjectCSRFToken added in v0.12.0

func InjectCSRFToken(r *http.Request, info *Info)

InjectCSRFToken adds the CSRF token to the form Info struct

func MapForm added in v0.8.0

func MapForm(r *http.Request, dst any, prefixes ...string) error

MapForm maps form values from an http.Request to a struct based on the `name` tag. Only exported fields are set. Supports string, int, float64, and bool fields.

func WeekStringToTime added in v0.11.0

func WeekStringToTime(weekStr string) (time.Time, error)

Types

type CSRFOptions added in v0.12.0

type CSRFOptions struct {
	// ErrorHandler lets you customize error handling instead of returning HTTP errors
	ErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
}

CSRFOptions configures how the CSRF middleware behaves

func DefaultCSRFOptions added in v0.12.0

func DefaultCSRFOptions() CSRFOptions

DefaultCSRFOptions returns the default options for CSRF protection

type DefaultLocalizer added in v0.7.0

type DefaultLocalizer struct{}

func (*DefaultLocalizer) GetLocale added in v0.7.0

func (d *DefaultLocalizer) GetLocale() string

type Enumerator

type Enumerator interface{ Enum() []any }

type FieldError

type FieldError interface {
	FieldError() (field, err string)
}

type FieldErrors added in v0.7.0

type FieldErrors []FieldError

type FieldValidationError added in v0.7.0

type FieldValidationError struct {
	Field string
	Err   string
}

func (FieldValidationError) Error added in v0.7.0

func (e FieldValidationError) Error() string

func (FieldValidationError) FieldError added in v0.7.0

func (e FieldValidationError) FieldError() (field, err string)

type Form

type Form struct {
	// contains filtered or unexported fields
}

func NewForm

func NewForm(templateMap map[types.FieldType]map[types.InputFieldType]string) *Form

NewForm creates a new form without translation support

func NewFormWithCSRF added in v0.12.0

func NewFormWithCSRF(templateMap map[types.FieldType]map[types.InputFieldType]string, store csrf.Store) *Form

NewFormWithCSRF creates a new form with CSRF protection

func NewTranslatedForm added in v0.7.0

func NewTranslatedForm(templateMap map[types.FieldType]map[types.InputFieldType]string, translationFunc TranslationFunc) *Form

NewTranslatedForm creates a new form with translation support

func (*Form) CSRFMiddleware added in v0.12.0

func (f *Form) CSRFMiddleware() func(next http.Handler) http.Handler

CSRFMiddleware creates middleware for CSRF protection with default options

func (*Form) CSRFMiddlewareWithOptions added in v0.12.0

func (f *Form) CSRFMiddlewareWithOptions(options CSRFOptions) func(next http.Handler) http.Handler

CSRFMiddlewareWithOptions creates middleware for CSRF protection with custom options

func (*Form) FuncMap

func (f *Form) FuncMap() template.FuncMap

func (*Form) GetCSRFStore added in v0.12.0

func (f *Form) GetCSRFStore() csrf.Store

GetCSRFStore returns the CSRF store

func (*Form) GetValidationMethod added in v0.7.0

func (f *Form) GetValidationMethod(name string) (ValidationFunc, bool)

func (*Form) HasCSRFStore added in v0.12.0

func (f *Form) HasCSRFStore() bool

HasCSRFStore checks if the form has a CSRF store

func (*Form) RegisterValidationMethod added in v0.7.0

func (f *Form) RegisterValidationMethod(name string, fn ValidationFunc)

func (*Form) SetCSRFStore added in v0.12.0

func (f *Form) SetCSRFStore(store csrf.Store)

SetCSRFStore sets the CSRF store for the Form

func (*Form) ValidateForm added in v0.7.0

func (f *Form) ValidateForm(form any) FieldErrors

func (*Form) ValidateFormLocalized added in v0.8.0

func (f *Form) ValidateFormLocalized(form any, loc Localizer) FieldErrors

type Info added in v0.6.0

type Info struct {
	Target     string `json:"target,omitempty"`
	Method     string `json:"method,omitempty"`
	SubmitText string `json:"submit,omitempty"`

	// CancelTarget enables an optional cancel action rendered by templates (as a link).
	// When empty, no cancel control is rendered.
	CancelTarget string `json:"cancel_target,omitempty"`
	// CancelText is the label for the cancel action. If empty and CancelTarget is set,
	// templates should fall back to a sensible default (e.g. "Cancel").
	CancelText string `json:"cancel_text,omitempty"`

	Attributes map[string]string `json:"attributes,omitempty"`
	CsrfValue  string            `json:"csrf_value,omitempty"` // CSRF token value
	CsrfField  string            `json:"csrf_field,omitempty"` // Name of the CSRF field (defaults to "_csrf")
}

type Localizer added in v0.7.0

type Localizer interface {
	// GetLocale returns the locale of the localizer, ie. "en_US"
	GetLocale() string
}

type MapFormError added in v0.8.0

type MapFormError struct {
	// contains filtered or unexported fields
}

func (*MapFormError) Error added in v0.8.0

func (e *MapFormError) Error() string

type Mapper added in v0.3.0

type Mapper interface {
	Mapper() map[string]string
	String() string
}

type MultiSelectGetter added in v0.19.0

type MultiSelectGetter interface {
	GetKeysAsStrings() []string
}

MultiSelectGetter provides a unified way to get selected keys as strings for all multi-select types.

type SortedMap added in v0.4.0

type SortedMap interface {
	Key() string
	Value() string
}

type SortedMapper added in v0.4.0

type SortedMapper interface {
	String() string
	SortedMapper() []SortedMap
}

SortedMapper is a new addition to provide sorted key value pairs

type SortedMultiSelect added in v0.19.0

type SortedMultiSelect[T comparable] struct {
	// contains filtered or unexported fields
}

SortedMultiSelect is a generic helper for multi-select dropdowns with custom key types. Holds a slice of selected values and a map of key->label.

func NewSortedMultiSelect added in v0.19.0

func NewSortedMultiSelect[T comparable](source map[T]string) SortedMultiSelect[T]

func (SortedMultiSelect[T]) Get added in v0.19.0

func (sms SortedMultiSelect[T]) Get() []T

Get returns the current selected values.

func (SortedMultiSelect[T]) GetKeysAsStrings added in v0.19.0

func (sms SortedMultiSelect[T]) GetKeysAsStrings() []string

GetKeysAsStrings returns the selected keys as strings.

func (SortedMultiSelect[T]) MarshalJSON added in v0.19.0

func (sms SortedMultiSelect[T]) MarshalJSON() ([]byte, error)

MarshalJSON outputs {"values":..., "source":...} with string keys for source

func (*SortedMultiSelect[T]) Scan added in v0.19.0

func (sms *SortedMultiSelect[T]) Scan(val any) error

Scan implements database/sql.Scanner for []T.

func (*SortedMultiSelect[T]) Set added in v0.19.0

func (sms *SortedMultiSelect[T]) Set(vals []T) error

Set sets the selected values, ensuring all exist in Source.

func (*SortedMultiSelect[T]) SetFromKeys added in v0.19.0

func (sms *SortedMultiSelect[T]) SetFromKeys(keys []string) error

SetFromKeys sets the selected values from a slice of string keys (as submitted by forms).

func (*SortedMultiSelect[T]) SetSource added in v0.19.0

func (sms *SortedMultiSelect[T]) SetSource(source map[T]string)

SetSource sets or updates the source map for SortedMultiSelect.

func (SortedMultiSelect[T]) SortedMapper added in v0.19.0

func (sms SortedMultiSelect[T]) SortedMapper() []SortedMap

SortedMapper returns entries sorted by key string representation.

func (SortedMultiSelect[T]) Source added in v0.19.0

func (sms SortedMultiSelect[T]) Source() map[T]string

Source returns a copy of the internal source map for read-only access.

func (SortedMultiSelect[T]) String added in v0.19.0

func (sms SortedMultiSelect[T]) String() string

String returns the selected values as a comma-separated string.

func (*SortedMultiSelect[T]) UnmarshalJSON added in v0.19.0

func (sms *SortedMultiSelect[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON restores values and Source, validates values

func (*SortedMultiSelect[T]) Value added in v0.19.0

func (sms *SortedMultiSelect[T]) Value() (any, error)

Value returns the selected values for DB storage.

type SortedSelect added in v0.17.0

type SortedSelect[T comparable] struct {
	// contains filtered or unexported fields
}

SortedSelect is a generic helper that holds a typed key value and a map of key->label It implements SortedMapper (value receiver) and a SetFromKey method (pointer receiver) so MapForm can set the selected value from the posted key string. T must be comparable to be used as map key.

func NewSortedSelect added in v0.17.0

func NewSortedSelect[T comparable](source map[T]string) SortedSelect[T]

func (SortedSelect[T]) Get added in v0.17.0

func (ss SortedSelect[T]) Get() T

Get returns the current value of the SortedSelect field (typed as T)

func (SortedSelect[T]) MarshalJSON added in v0.17.0

func (ss SortedSelect[T]) MarshalJSON() ([]byte, error)

MarshalJSON outputs {"value":..., "source":...} with string keys for source

func (*SortedSelect[T]) Scan added in v0.17.0

func (ss *SortedSelect[T]) Scan(val any) error

func (*SortedSelect[T]) Set added in v0.17.0

func (ss *SortedSelect[T]) Set(val T) error

Set sets the value of the SortedSelect field, ensuring it exists in Source if Source is not nil

func (*SortedSelect[T]) SetFromKey added in v0.17.0

func (ss *SortedSelect[T]) SetFromKey(key string) error

SetFromKey sets the value from a string key (as submitted by forms)

func (*SortedSelect[T]) SetSource added in v0.17.0

func (ss *SortedSelect[T]) SetSource(source map[T]string)

SetSource sets or updates the source map for SortedSelect

func (SortedSelect[T]) SortedMapper added in v0.17.0

func (ss SortedSelect[T]) SortedMapper() []SortedMap

SortedMapper returns entries sorted by key string representation Uses fmt.Sprint to produce string representations of keys (no converters required).

func (SortedSelect[T]) Source added in v0.17.0

func (ss SortedSelect[T]) Source() map[T]string

Source returns a copy of the internal source map for read-only access

func (SortedSelect[T]) String added in v0.17.0

func (ss SortedSelect[T]) String() string

String returns the current value as a string using fmt.Sprint (no converters required)

func (*SortedSelect[T]) UnmarshalJSON added in v0.17.0

func (ss *SortedSelect[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON restores value and Source, validates value

func (*SortedSelect[T]) Value added in v0.17.0

func (ss *SortedSelect[T]) Value() (any, error)

type SortedSelectError added in v0.17.0

type SortedSelectError struct {
	Key  string
	Args []any
}

SortedSelectError is a structured error for translation Holds the translation key and arguments for formatting Implements error interface Usage: return SortedSelectError{Key: key, Args: args}

func (SortedSelectError) Error added in v0.17.0

func (e SortedSelectError) Error() string

type Transformer

type Transformer struct {
	Fields []types.FormField `json:"fields"`
}

func NewTransformer

func NewTransformer(model interface{}) (*Transformer, error)

func (*Transformer) JSON

func (t *Transformer) JSON() json.RawMessage

type TranslationFunc added in v0.7.0

type TranslationFunc func(loc Localizer, key string, args ...any) string

type ValidationFunc added in v0.7.0

type ValidationFunc func(fieldValue any, fieldStruct reflect.StructField) FieldErrors

Directories

Path Synopsis
example
csrf command
sortedselect command
templates command
translation command
validation command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL