Documentation
¶
Index ¶
- Constants
- func AssertFieldContains(t *testing.T, field FieldInterface, expected string)
- func AssertFieldNotContains(t *testing.T, field FieldInterface, unexpected string)
- func AssertFormContains(t *testing.T, form *Form, expected string)
- func AssertFormNotContains(t *testing.T, form *Form, unexpected string)
- func AssertValidationErrorCount(t *testing.T, form *Form, values map[string]string, expectedCount int)
- func AssertValidationFailsOn(t *testing.T, form *Form, values map[string]string, fieldName string)
- func AssertValidationPasses(t *testing.T, form *Form, values map[string]string)
- func NewFieldRow(fields ...FieldInterface) *fieldRow
- func NewFieldRowWithColumns(columns ...FieldRowColumn) *fieldRow
- func NewRepeater(opts RepeaterOptions) *fieldRepeater
- type Field
- func NewCheckboxField(name, label string) *Field
- func NewColorField(name, label string) *Field
- func NewDateField(name, label string) *Field
- func NewDateTimeField(name, label string) *Field
- func NewEmailField(name, label string) *Field
- func NewField(opts FieldOptions) *Field
- func NewFileField(name, label string) *Field
- func NewHiddenField(name, value string) *Field
- func NewHtmlAreaField(name, label string) *Field
- func NewImageField(name, label string) *Field
- func NewNumberField(name, label string) *Field
- func NewPasswordField(name, label string) *Field
- func NewRadioField(name, label string, options []FieldOption) *Field
- func NewRawField(value string) *Field
- func NewSelectField(name, label string, options []FieldOption) *Field
- func NewStringField(name, label string) *Field
- func NewTelField(name, label string) *Field
- func NewTextAreaField(name, label string) *Field
- func NewURLField(name, label string) *Field
- func (field *Field) BuildFormGroup(fileManagerURL string) *hb.Tag
- func (field *Field) GetHelp() string
- func (field *Field) GetID() string
- func (field *Field) GetLabel() string
- func (field *Field) GetName() string
- func (field *Field) GetOptions() []FieldOption
- func (field *Field) GetOptionsF() func() []FieldOption
- func (field *Field) GetRequired() bool
- func (field *Field) GetType() string
- func (field *Field) GetValue() string
- func (field *Field) IsBlockEditor() bool
- func (field *Field) IsCheckbox() bool
- func (field *Field) IsColor() bool
- func (field *Field) IsDate() bool
- func (field *Field) IsDateTime() bool
- func (field *Field) IsDisabled() bool
- func (field *Field) IsEmail() bool
- func (field *Field) IsFile() bool
- func (field *Field) IsHidden() bool
- func (field *Field) IsHtmlArea() bool
- func (field *Field) IsImage() bool
- func (field *Field) IsNumber() bool
- func (field *Field) IsPassword() bool
- func (field *Field) IsRadio() bool
- func (field *Field) IsRaw() bool
- func (field *Field) IsReadonly() bool
- func (field *Field) IsRequired() bool
- func (field *Field) IsSelect() bool
- func (field *Field) IsString() bool
- func (field *Field) IsTable() bool
- func (field *Field) IsTel() bool
- func (field *Field) IsTextArea() bool
- func (field *Field) IsUrl() bool
- func (field *Field) SetHelp(fieldHelp string)
- func (field *Field) SetID(fieldID string)
- func (field *Field) SetLabel(fieldLabel string)
- func (field *Field) SetName(fieldName string)
- func (field *Field) SetOptions(fieldOptions []FieldOption)
- func (field *Field) SetOptionsF(fieldOptionsF func() []FieldOption)
- func (field *Field) SetRequired(fieldRequired bool)
- func (field *Field) SetType(fieldType string)
- func (field *Field) SetValue(fieldValue string)
- func (field *Field) TrumbowygScript() string
- func (field *Field) WithAttr(key, value string) *Field
- func (field *Field) WithAttrs(attrs map[string]string) *Field
- func (field *Field) WithCustomInput(input hb.TagInterface) *Field
- func (field *Field) WithDisabled() *Field
- func (field *Field) WithHelp(help string) *Field
- func (field *Field) WithID(id string) *Field
- func (field *Field) WithInvisible() *Field
- func (field *Field) WithLabel(label string) *Field
- func (field *Field) WithMultiple() *Field
- func (field *Field) WithName(name string) *Field
- func (field *Field) WithOptions(options ...FieldOption) *Field
- func (field *Field) WithOptionsF(optionsF func() []FieldOption) *Field
- func (field *Field) WithPlaceholder(placeholder string) *Field
- func (field *Field) WithReadonly() *Field
- func (field *Field) WithRequired() *Field
- func (field *Field) WithTableOptions(opts TableOptions) *Field
- func (field *Field) WithType(fieldType string) *Field
- func (field *Field) WithValidators(validators ...Validator) *Field
- func (field *Field) WithValue(value string) *Field
- type FieldInterface
- type FieldOption
- type FieldOptions
- type FieldRowColumn
- type Form
- func (form *Form) AddField(field FieldInterface)
- func (form *Form) Build() *hb.Tag
- func (form *Form) GetErrors() map[string]string
- func (form *Form) GetFields() []FieldInterface
- func (form *Form) GetFileManagerURL() string
- func (form *Form) SetErrors(errors map[string]string)
- func (form *Form) SetFields(fields []FieldInterface)
- func (form *Form) SetFileManagerURL(url string)
- func (form *Form) Validate(values map[string]string) []ValidationError
- func (form *Form) WithAction(actionURL string) *Form
- func (form *Form) WithClass(className string) *Form
- func (form *Form) WithErrors(errors map[string]string) *Form
- func (form *Form) WithFields(fields ...FieldInterface) *Form
- func (form *Form) WithFileManager(url string) *Form
- func (form *Form) WithHTMX(config HTMXConfig) *Form
- func (form *Form) WithHxPost(url string) *Form
- func (form *Form) WithHxSwap(swap string) *Form
- func (form *Form) WithHxTarget(target string) *Form
- func (form *Form) WithID(id string) *Form
- func (form *Form) WithMethod(method string) *Form
- func (form *Form) WithTheme(theme *Theme) *Form
- type FormOptions
- type HTMXConfig
- type RepeaterOptions
- type TableColumn
- type TableOptions
- type Theme
- type ValidationError
- type Validator
- func ValidatorAlphaNumeric() Validator
- func ValidatorCustom(fn func(value string) string) Validator
- func ValidatorEmail() Validator
- func ValidatorIP() Validator
- func ValidatorMax(max float64) Validator
- func ValidatorMaxLength(maxLength int) Validator
- func ValidatorMin(min float64) Validator
- func ValidatorMinLength(minLength int) Validator
- func ValidatorOneOf(allowed ...string) Validator
- func ValidatorPattern(pattern string, message string) Validator
- func ValidatorRequired() Validator
- func ValidatorURL() Validator
- func ValidatorUUID() Validator
Constants ¶
const FORM_FIELD_TYPE_BLOCKEDITOR = "blockeditor"
const FORM_FIELD_TYPE_CHECKBOX = "checkbox"
const FORM_FIELD_TYPE_COLOR = "color"
const FORM_FIELD_TYPE_DATE = "date"
const FORM_FIELD_TYPE_DATETIME = "datetime"
const FORM_FIELD_TYPE_EMAIL = "email"
const FORM_FIELD_TYPE_FILE = "file"
const FORM_FIELD_TYPE_HIDDEN = "hidden"
const FORM_FIELD_TYPE_HTMLAREA = "htmlarea"
const FORM_FIELD_TYPE_IMAGE = "image"
const FORM_FIELD_TYPE_NUMBER = "number"
const FORM_FIELD_TYPE_PASSWORD = "password"
const FORM_FIELD_TYPE_RADIO = "radio"
const FORM_FIELD_TYPE_RAW = "raw"
const FORM_FIELD_TYPE_SELECT = "select"
const FORM_FIELD_TYPE_STRING = "string"
const FORM_FIELD_TYPE_TABLE = "table"
const FORM_FIELD_TYPE_TEL = "tel"
const FORM_FIELD_TYPE_TEXTAREA = "textarea"
const FORM_FIELD_TYPE_URL = "url"
Variables ¶
This section is empty.
Functions ¶
func AssertFieldContains ¶ added in v0.20.0
func AssertFieldContains(t *testing.T, field FieldInterface, expected string)
AssertFieldContains checks that the rendered field HTML contains the expected string.
func AssertFieldNotContains ¶ added in v0.20.0
func AssertFieldNotContains(t *testing.T, field FieldInterface, unexpected string)
AssertFieldNotContains checks that the rendered field HTML does NOT contain the given string.
func AssertFormContains ¶ added in v0.20.0
AssertFormContains checks that the rendered form HTML contains the expected string. It calls t.Helper() so the test failure points to the caller.
func AssertFormNotContains ¶ added in v0.20.0
AssertFormNotContains checks that the rendered form HTML does NOT contain the given string.
func AssertValidationErrorCount ¶ added in v0.20.0
func AssertValidationErrorCount(t *testing.T, form *Form, values map[string]string, expectedCount int)
AssertValidationErrorCount checks that validation produces exactly n errors.
func AssertValidationFailsOn ¶ added in v0.20.0
AssertValidationFailsOn checks that validation fails specifically on the named field.
func AssertValidationPasses ¶ added in v0.20.0
AssertValidationPasses checks that the form validation passes for the given values.
func NewFieldRow ¶ added in v0.20.0
func NewFieldRow(fields ...FieldInterface) *fieldRow
NewFieldRow creates a row of fields. Each field gets an equal-width column by default.
func NewFieldRowWithColumns ¶ added in v0.20.0
func NewFieldRowWithColumns(columns ...FieldRowColumn) *fieldRow
NewFieldRowWithColumns creates a row with explicit column configurations.
func NewRepeater ¶
func NewRepeater(opts RepeaterOptions) *fieldRepeater
NewRepeater creates a new repeater field that allows adding and removing groups of fields.
Types ¶
type Field ¶
type Field struct {
ID string // automatic, if not assigned
Type string
Name string
Label string
Help string
Options []FieldOption
OptionsF func() []FieldOption
Value string
Required bool
Readonly bool
Disabled bool
TableOptions TableOptions
// BlockEditorOptions BlockEditorOptions
Placeholder string
Invisible bool
CustomInput hb.TagInterface
Attrs map[string]string
Multiple bool
Validators []Validator
// contains filtered or unexported fields
}
Field represents a single form field with its configuration, value, and rendering options.
func NewCheckboxField ¶ added in v0.20.0
NewCheckboxField creates a new checkbox field with the given name and label.
func NewColorField ¶ added in v0.20.0
NewColorField creates a new color picker field with the given name and label.
func NewDateField ¶ added in v0.20.0
NewDateField creates a new date input field with the given name and label.
func NewDateTimeField ¶ added in v0.20.0
NewDateTimeField creates a new datetime input field with the given name and label.
func NewEmailField ¶ added in v0.20.0
NewEmailField creates a new email input field with the given name and label.
func NewField ¶
func NewField(opts FieldOptions) *Field
NewField creates a new Field with the given options.
func NewFileField ¶ added in v0.20.0
NewFileField creates a new file upload field with the given name and label.
func NewHiddenField ¶ added in v0.20.0
NewHiddenField creates a new hidden input field with the given name and value.
func NewHtmlAreaField ¶ added in v0.20.0
NewHtmlAreaField creates a new HTML area (WYSIWYG) field with the given name and label.
func NewImageField ¶ added in v0.20.0
NewImageField creates a new image field with the given name and label.
func NewNumberField ¶ added in v0.20.0
NewNumberField creates a new number input field with the given name and label.
func NewPasswordField ¶ added in v0.20.0
NewPasswordField creates a new password input field with the given name and label.
func NewRadioField ¶ added in v0.20.0
func NewRadioField(name, label string, options []FieldOption) *Field
NewRadioField creates a new radio button group with the given name, label, and options.
func NewRawField ¶ added in v0.20.0
NewRawField creates a new raw HTML field with the given value (rendered as-is).
func NewSelectField ¶ added in v0.20.0
func NewSelectField(name, label string, options []FieldOption) *Field
NewSelectField creates a new select field with the given name, label, and options.
func NewStringField ¶ added in v0.20.0
NewStringField creates a new text input field with the given name and label.
func NewTelField ¶ added in v0.20.0
NewTelField creates a new telephone input field with the given name and label.
func NewTextAreaField ¶ added in v0.20.0
NewTextAreaField creates a new textarea field with the given name and label.
func NewURLField ¶ added in v0.20.0
NewURLField creates a new URL input field with the given name and label.
func (*Field) BuildFormGroup ¶
BuildFormGroup builds the complete form group HTML element for this field, including label, input, help text, and any required indicators.
func (*Field) GetOptions ¶
func (field *Field) GetOptions() []FieldOption
func (*Field) GetOptionsF ¶
func (field *Field) GetOptionsF() func() []FieldOption
func (*Field) GetRequired ¶
func (*Field) IsBlockEditor ¶
func (*Field) IsCheckbox ¶ added in v0.20.0
func (*Field) IsDateTime ¶
func (*Field) IsDisabled ¶
func (*Field) IsHtmlArea ¶
func (*Field) IsPassword ¶
func (*Field) IsReadonly ¶
func (*Field) IsRequired ¶
func (*Field) IsTextArea ¶
func (*Field) SetOptions ¶
func (field *Field) SetOptions(fieldOptions []FieldOption)
func (*Field) SetOptionsF ¶
func (field *Field) SetOptionsF(fieldOptionsF func() []FieldOption)
func (*Field) SetRequired ¶
func (*Field) TrumbowygScript ¶
TrumbowygScript returns the JavaScript code to initialize the Trumbowyg WYSIWYG editor for this field, using either the provided config option or a default configuration.
func (*Field) WithAttr ¶ added in v0.20.0
WithAttr sets a single custom HTML attribute on the field's input element.
func (*Field) WithAttrs ¶ added in v0.20.0
WithAttrs sets multiple custom HTML attributes on the field's input element.
func (*Field) WithCustomInput ¶ added in v0.20.0
func (field *Field) WithCustomInput(input hb.TagInterface) *Field
WithCustomInput sets a custom hb.Tag to use as the input element.
func (*Field) WithDisabled ¶ added in v0.20.0
WithDisabled marks the field as disabled.
func (*Field) WithHelp ¶ added in v0.20.0
WithHelp sets the field's help text displayed below the input.
func (*Field) WithInvisible ¶ added in v0.20.0
WithInvisible marks the field as invisible (hidden via CSS).
func (*Field) WithMultiple ¶ added in v0.20.0
WithMultiple enables multi-select on select fields.
func (*Field) WithOptions ¶ added in v0.20.0
func (field *Field) WithOptions(options ...FieldOption) *Field
WithOptions sets the field's static options (for select, radio, etc.).
func (*Field) WithOptionsF ¶ added in v0.20.0
func (field *Field) WithOptionsF(optionsF func() []FieldOption) *Field
WithOptionsF sets a dynamic options provider function.
func (*Field) WithPlaceholder ¶ added in v0.20.0
WithPlaceholder sets the field's placeholder text.
func (*Field) WithReadonly ¶ added in v0.20.0
WithReadonly marks the field as readonly.
func (*Field) WithRequired ¶ added in v0.20.0
WithRequired marks the field as required.
func (*Field) WithTableOptions ¶ added in v0.20.0
func (field *Field) WithTableOptions(opts TableOptions) *Field
WithTableOptions sets the table options for table-type fields.
func (*Field) WithValidators ¶ added in v0.20.0
WithValidators sets the field's validators.
type FieldInterface ¶
type FieldInterface interface {
GetID() string
SetID(fieldID string)
GetLabel() string
SetLabel(fieldLabel string)
GetName() string
SetName(fieldName string)
GetHelp() string
SetHelp(fieldName string)
GetOptions() []FieldOption
SetOptions(fieldOptions []FieldOption)
GetOptionsF() func() []FieldOption
SetOptionsF(fieldOptionsF func() []FieldOption)
GetRequired() bool
SetRequired(fieldRequired bool)
GetType() string
SetType(fieldType string)
GetValue() string
SetValue(fieldValue string)
BuildFormGroup(fileManagerURL string) *hb.Tag
// contains filtered or unexported methods
}
FieldInterface defines the contract that all form field types must implement.
type FieldOption ¶
FieldOption represents a key-value pair used for select, radio, and other option-based fields.
type FieldOptions ¶
type FieldOptions struct {
ID string // automatic, if not assigned
Type string
Name string
Label string
Help string
Options []FieldOption
OptionsF func() []FieldOption
Value string
Required bool
Readonly bool
Disabled bool
TableOptions TableOptions
Placeholder string
Invisible bool
CustomInput hb.TagInterface
Attrs map[string]string
Multiple bool
Validators []Validator
}
FieldOptions configures a new Field instance.
type FieldRowColumn ¶ added in v0.20.0
type FieldRowColumn struct {
Field FieldInterface
ColClass string // e.g. "col-md-6", "col-4". Empty = auto equal width ("col")
}
FieldRowColumn represents a single column in a field row with its field and optional CSS class.
type Form ¶
type Form struct {
// contains filtered or unexported fields
}
Form represents an HTML form with fields, styling, and optional HTMX integration.
func New ¶ added in v0.20.0
func New() *Form
New creates a new empty Form with POST as the default method. Use the With* methods to configure it via chaining.
func NewForm ¶
func NewForm(opts FormOptions) *Form
NewForm creates a new Form with the given options. Defaults to POST method if not specified.
func (*Form) AddField ¶
func (form *Form) AddField(field FieldInterface)
AddField appends a field to the form.
func (*Form) GetFields ¶
func (form *Form) GetFields() []FieldInterface
GetFields returns all fields in the form.
func (*Form) GetFileManagerURL ¶
GetFileManagerURL returns the file manager URL used for image fields.
func (*Form) SetErrors ¶ added in v0.20.0
SetErrors sets the validation error messages to display inline next to fields. The map keys are field names, values are error messages.
func (*Form) SetFields ¶
func (form *Form) SetFields(fields []FieldInterface)
SetFields replaces all fields in the form.
func (*Form) SetFileManagerURL ¶
SetFileManagerURL sets the file manager URL used for image fields.
func (*Form) Validate ¶ added in v0.20.0
func (form *Form) Validate(values map[string]string) []ValidationError
Validate validates the given values against the form fields and their validators. It returns a slice of ValidationError. An empty slice means validation passed. Errors are also stored on the form for inline display when Build() is called.
func (*Form) WithAction ¶ added in v0.20.0
WithAction sets the form's action URL.
func (*Form) WithErrors ¶ added in v0.20.0
WithErrors sets validation error messages to display inline next to fields. The map keys are field names, values are error messages.
func (*Form) WithFields ¶ added in v0.20.0
func (form *Form) WithFields(fields ...FieldInterface) *Form
WithFields sets the form's fields.
func (*Form) WithFileManager ¶ added in v0.20.0
WithFileManager sets the file manager URL used for image fields.
func (*Form) WithHTMX ¶ added in v0.20.0
func (form *Form) WithHTMX(config HTMXConfig) *Form
WithHTMX sets HTMX attributes on the form using a structured config. This provides access to extended HTMX attributes beyond Post/Target/Swap.
func (*Form) WithHxPost ¶ added in v0.20.0
WithHxPost sets the hx-post attribute for HTMX integration.
func (*Form) WithHxSwap ¶ added in v0.20.0
WithHxSwap sets the hx-swap attribute for HTMX integration.
func (*Form) WithHxTarget ¶ added in v0.20.0
WithHxTarget sets the hx-target attribute for HTMX integration.
func (*Form) WithMethod ¶ added in v0.20.0
WithMethod sets the form's HTTP method.
type FormOptions ¶
type FormOptions struct {
ActionURL string // optional
ClassName string // optional
ID string // optional
Fields []FieldInterface // optional
FileManagerURL string // optional
Method string // optional
// HTMX helpers
HxPost string // optional
HxTarget string // optional
HxSwap string // optional
}
FormOptions configures a new Form instance.
type HTMXConfig ¶ added in v0.20.0
type HTMXConfig struct {
Post string // hx-post URL
Get string // hx-get URL
Target string // hx-target selector
Swap string // hx-swap method
Trigger string // hx-trigger event
Indicator string // hx-indicator selector
Confirm string // hx-confirm message
Sync string // hx-sync strategy
Validate bool // hx-validate
DisabledElt string // hx-disabled-elt selector
Encoding string // hx-encoding (e.g. "multipart/form-data")
PushURL string // hx-push-url
}
HTMXConfig provides a structured way to configure HTMX attributes on a form.
type RepeaterOptions ¶
type RepeaterOptions struct {
Label string
Type string
Name string
Value string
Help string
Fields []FieldInterface
Values []map[string]string
RepeaterAddUrl string
RepeaterMoveUpUrl string
RepeaterMoveDownUrl string
RepeaterRemoveUrl string
}
RepeaterOptions configures a new repeater field instance.
type TableColumn ¶
TableColumn represents a column header in a table field.
type TableOptions ¶
type TableOptions struct {
Header []TableColumn
Rows [][]Field
RowAddButton *hb.Tag
RowDeleteButton *hb.Tag
}
TableOptions configures the layout and behavior of a table field.
type Theme ¶ added in v0.20.0
type Theme struct {
FormGroupClass string
LabelClass string
InputClass string
SelectClass string
TextAreaClass string
CheckboxWrapClass string
CheckboxInputClass string
RadioWrapClass string
RadioInputClass string
RadioLabelClass string
FileInputClass string
HelpClass string
RequiredClass string
RequiredMarker string
TableClass string
ErrorClass string // CSS class for the error message element
ErrorInputClass string // CSS class added to invalid inputs
}
Theme defines the CSS classes used when rendering form fields. This allows decoupling from any specific CSS framework.
func ThemeBootstrap5 ¶ added in v0.20.0
func ThemeBootstrap5() *Theme
ThemeBootstrap5 returns the default Bootstrap 5 theme.
func ThemeTailwind ¶ added in v0.20.0
func ThemeTailwind() *Theme
ThemeTailwind returns a Tailwind CSS theme.
type ValidationError ¶ added in v0.20.0
ValidationError represents a single validation error for a field.
func AssertValidationFails ¶ added in v0.20.0
AssertValidationFails checks that the form validation fails for the given values. It returns the validation errors for further inspection.
type Validator ¶ added in v0.20.0
type Validator func(fieldName string, value string) *ValidationError
Validator is a function that validates a field value and returns an error message if invalid.
func ValidatorAlphaNumeric ¶ added in v0.20.0
func ValidatorAlphaNumeric() Validator
ValidatorAlphaNumeric returns a validator that checks if a value contains only letters and numbers.
func ValidatorCustom ¶ added in v0.20.0
ValidatorCustom returns a validator that uses a custom function. The function receives the value and returns an error message if invalid, or empty string if valid.
func ValidatorEmail ¶ added in v0.20.0
func ValidatorEmail() Validator
ValidatorEmail returns a validator that checks if a value is a valid email address.
func ValidatorIP ¶ added in v0.20.0
func ValidatorIP() Validator
ValidatorIP returns a validator that checks if a value is a valid IPv4 address.
func ValidatorMax ¶ added in v0.20.0
ValidatorMax returns a validator that checks if a numeric value is at most max.
func ValidatorMaxLength ¶ added in v0.20.0
ValidatorMaxLength returns a validator that checks if a value has at most maxLength characters.
func ValidatorMin ¶ added in v0.20.0
ValidatorMin returns a validator that checks if a numeric value is at least min.
func ValidatorMinLength ¶ added in v0.20.0
ValidatorMinLength returns a validator that checks if a value has at least minLength characters.
func ValidatorOneOf ¶ added in v0.20.0
ValidatorOneOf returns a validator that checks if a value is one of the allowed values.
func ValidatorPattern ¶ added in v0.20.0
ValidatorPattern returns a validator that checks if a value matches a regex pattern.
func ValidatorRequired ¶ added in v0.20.0
func ValidatorRequired() Validator
ValidatorRequired returns a validator that checks if a value is non-empty.
func ValidatorURL ¶ added in v0.20.0
func ValidatorURL() Validator
ValidatorURL returns a validator that checks if a value is a valid URL.
func ValidatorUUID ¶ added in v0.20.0
func ValidatorUUID() Validator
ValidatorUUID returns a validator that checks if a value is a valid UUID.