Documentation
¶
Index ¶
- Variables
- func GetCSRFToken(r *http.Request) (string, bool)
- func InjectCSRFToken(r *http.Request, info *Info)
- func MapForm(r *http.Request, dst any, prefixes ...string) error
- func WeekStringToTime(weekStr string) (time.Time, error)
- type CSRFOptions
- type DefaultLocalizer
- type Enumerator
- type FieldError
- type FieldErrors
- type FieldValidationError
- type Form
- func (f *Form) CSRFMiddleware() func(next http.Handler) http.Handler
- func (f *Form) CSRFMiddlewareWithOptions(options CSRFOptions) func(next http.Handler) http.Handler
- func (f *Form) FuncMap() template.FuncMap
- func (f *Form) GetCSRFStore() csrf.Store
- func (f *Form) GetValidationMethod(name string) (ValidationFunc, bool)
- func (f *Form) HasCSRFStore() bool
- func (f *Form) RegisterValidationMethod(name string, fn ValidationFunc)
- func (f *Form) SetCSRFStore(store csrf.Store)
- func (f *Form) ValidateForm(form any) FieldErrors
- func (f *Form) ValidateFormLocalized(form any, loc Localizer) FieldErrors
- type Info
- type Localizer
- type MapFormError
- type Mapper
- type MultiSelectGetter
- type SortedMap
- type SortedMapper
- type SortedMultiSelect
- func (sms SortedMultiSelect[T]) Get() []T
- func (sms SortedMultiSelect[T]) GetKeysAsStrings() []string
- func (sms SortedMultiSelect[T]) MarshalJSON() ([]byte, error)
- func (sms *SortedMultiSelect[T]) Scan(val any) error
- func (sms *SortedMultiSelect[T]) Set(vals []T) error
- func (sms *SortedMultiSelect[T]) SetFromKeys(keys []string) error
- func (sms *SortedMultiSelect[T]) SetSource(source map[T]string)
- func (sms SortedMultiSelect[T]) SortedMapper() []SortedMap
- func (sms SortedMultiSelect[T]) Source() map[T]string
- func (sms SortedMultiSelect[T]) String() string
- func (sms *SortedMultiSelect[T]) UnmarshalJSON(data []byte) error
- func (sms *SortedMultiSelect[T]) Value() (any, error)
- type SortedSelect
- func (ss SortedSelect[T]) Get() T
- func (ss SortedSelect[T]) MarshalJSON() ([]byte, error)
- func (ss *SortedSelect[T]) Scan(val any) error
- func (ss *SortedSelect[T]) Set(val T) error
- func (ss *SortedSelect[T]) SetFromKey(key string) error
- func (ss *SortedSelect[T]) SetSource(source map[T]string)
- func (ss SortedSelect[T]) SortedMapper() []SortedMap
- func (ss SortedSelect[T]) Source() map[T]string
- func (ss SortedSelect[T]) String() string
- func (ss *SortedSelect[T]) UnmarshalJSON(data []byte) error
- func (ss *SortedSelect[T]) Value() (any, error)
- type SortedSelectError
- type Transformer
- type TranslationFunc
- type ValidationFunc
Constants ¶
This section is empty.
Variables ¶
var ( ErrMapFormNotPointer = &MapFormError{"dst must be a pointer to struct"} ErrMapFormNotStruct = &MapFormError{"dst must be a pointer to struct"} )
var ( DefaultSubmitText = "Submit" DefaultEnumTranslation = false // Set to true to enable translation for all enums by default )
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" )
var (
DefaultCSRFField = "_csrf"
)
CSRF errors
Functions ¶
func GetCSRFToken ¶ added in v0.12.0
GetCSRFToken retrieves the CSRF token from the request context
func InjectCSRFToken ¶ added in v0.12.0
InjectCSRFToken adds the CSRF token to the form Info struct
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
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 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
CSRFMiddleware creates middleware for CSRF protection with default options
func (*Form) CSRFMiddlewareWithOptions ¶ added in v0.12.0
CSRFMiddlewareWithOptions creates middleware for CSRF protection with custom options
func (*Form) GetCSRFStore ¶ added in v0.12.0
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
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
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 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 SortedMapper ¶ added in v0.4.0
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
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 ¶
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 ValidationFunc ¶ added in v0.7.0
type ValidationFunc func(fieldValue any, fieldStruct reflect.StructField) FieldErrors
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
example
|
|
|
csrf
command
|
|
|
sortedselect
command
|
|
|
templates
command
|
|
|
translation
command
|
|
|
validation
command
|
|