Documentation
¶
Overview ¶
templ: version: v0.3.1036
templ: version: v0.3.1036
Package forms provides accessible, CSP-compliant form controls for Go templ applications.
Components include text inputs, selects, textareas, checkboxes, labels, and validation error helpers. All controls support error states with proper ARIA attributes.
templ: version: v0.3.1036
templ: version: v0.3.1036
Package forms provides utility functions for form components.
templ: version: v0.3.1036
templ: version: v0.3.1036
templ: version: v0.3.1036
Package forms provides radio button components.
templ: version: v0.3.1036
templ: version: v0.3.1036
templ: version: v0.3.1036
templ: version: v0.3.1036
templ: version: v0.3.1036
Index ¶
- func Checkbox(props CheckboxProps) templ.Component
- func Combobox(props ComboboxProps) templ.Component
- func DatePicker(props DatePickerProps) templ.Component
- func ErrorAttrs(id, errMsg, helpTextID string) templ.Attributes
- func FieldError(fieldID, message string) templ.Component
- func FileInput(props FileInputProps) templ.Component
- func Form(props FormProps) templ.Component
- func FormFieldWrapper(props FormFieldProps) templ.Component
- func HelpTextID(id string) string
- func Input(props InputProps) templ.Component
- func InputGroup(props InputGroupProps) templ.Component
- func InputGroupPaddingClass(left, right bool) string
- func Label(forID, text string, required bool) templ.Component
- func Radio(props RadioProps) templ.Component
- func RadioGroup(props RadioGroupProps) templ.Component
- func SanitizeID(s string) string
- func Select(props SelectProps) templ.Component
- func Textarea(props TextareaProps) templ.Component
- func Toggle(props ToggleProps) templ.Component
- func ValidationSummary(props ValidationSummaryProps) templ.Component
- type CheckboxProps
- type ComboboxOption
- type ComboboxProps
- type DatePickerProps
- type FileInputProps
- type FormFieldProps
- type FormMethod
- type FormProps
- type InputGroupProps
- type InputProps
- type InputType
- type RadioGroupProps
- type RadioOption
- type RadioProps
- type SelectOption
- type SelectProps
- type TextareaProps
- type ToggleProps
- type ToggleSize
- type ValidationError
- type ValidationSummaryProps
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Checkbox ¶
func Checkbox(props CheckboxProps) templ.Component
Checkbox renders a checkbox with label
@forms.Checkbox(forms.CheckboxProps{Name: "terms", ID: "terms", Label: "I agree to the terms"})
func Combobox ¶ added in v0.4.0
func Combobox(props ComboboxProps) templ.Component
Combobox renders an accessible autocomplete input with a filterable dropdown. As the user types, options are filtered client-side. Clicking an option fills the input and sets the hidden value for form submission.
@forms.Combobox(forms.ComboboxProps{
Name: "country",
ID: "country",
Label: "Country",
Placeholder: "Search countries...",
Options: []forms.ComboboxOption{
{Value: "de", Label: "Germany"},
{Value: "at", Label: "Austria"},
},
})
func DatePicker ¶ added in v0.4.0
func DatePicker(props DatePickerProps) templ.Component
DatePicker renders a labeled date input with optional min/max constraints, error, and help text
@forms.DatePicker(forms.DatePickerProps{
Name: "birthday",
ID: "birthday",
Label: "Date of Birth",
Required: true,
})
func ErrorAttrs ¶
func ErrorAttrs(id, errMsg, helpTextID string) templ.Attributes
ErrorAttrs returns templ attributes for aria-invalid and aria-describedby when an error message or help text is present. Returns nil only when both errMsg and helpTextID are empty. When helpTextID is non-empty it is always emitted via aria-describedby (independent of id), so id-less fields with help text remain accessible to assistive technology.
func FieldError ¶
FieldError renders a field-level validation error
@forms.FieldError("email", "This field is required")
func FileInput ¶
func FileInput(props FileInputProps) templ.Component
FileInput renders a labeled file input with styled native button
@forms.FileInput(forms.FileInputProps{
Name: "avatar",
Label: "Upload avatar",
Accept: "image/*",
})
func Form ¶
Form renders a form element with optional CSRF token
@forms.Form(forms.FormProps{Action: "/submit", CSRFToken: "token123"}) {
@forms.Input(forms.InputProps{Name: "email"})
}
Example ¶
_ = Form(FormProps{
Action: "/api/users",
Method: FormPost,
CSRFToken: "example-token",
})
func FormFieldWrapper ¶
func FormFieldWrapper(props FormFieldProps) templ.Component
FormFieldWrapper renders the standard form field structure: optional Label, children (input/select/textarea), optional Error, optional HelpText. Use this to DRY up all form control components.
@forms.FormFieldWrapper(forms.FormFieldProps{ID: "email", Label: "Email", Required: true, Error: "Invalid", HelpText: "Use work email"}) {
<input type="email" name="email" />
}
func HelpTextID ¶
HelpTextID returns the HTML ID for a help text element, or "" if id is empty.
func Input ¶
func Input(props InputProps) templ.Component
Input renders a labeled text input with optional error and help text
@forms.Input(forms.InputProps{
Name: "email",
Type: forms.InputEmail,
Label: "Email address",
Required: true,
})
Example ¶
_ = Input(InputProps{
Name: "email",
Type: InputEmail,
Label: "Email address",
Placeholder: "you@example.com",
HelpText: "We'll never share your email.",
})
func InputGroup ¶
func InputGroup(props InputGroupProps) templ.Component
InputGroup renders a form control with optional left/right addons. Addons are absolutely positioned inside the input area.
@forms.InputGroup(forms.InputGroupProps{
LeftAddon: icons.Icon(icons.Search, "h-5 w-5 text-gray-400"),
}) {
@forms.Input(forms.InputProps{Class: "pl-10"})
}
func InputGroupPaddingClass ¶
InputGroupPaddingClass returns the appropriate padding class for an input that lives inside an InputGroup with the given addons. Call this from your input's Class field to avoid manual padding:
@forms.InputGroup(forms.InputGroupProps{LeftAddon: icon}) {
@forms.Input(forms.InputProps{Class: forms.InputGroupPaddingClass(true, false)})
}
func Label ¶
Label renders a form label with optional required indicator
@forms.Label("email", "Email address", true)
func Radio ¶
func Radio(props RadioProps) templ.Component
Radio renders a single radio button with label
@forms.Radio(forms.RadioProps{
ID: "plan-pro",
Name: "plan",
Value: "pro",
Label: "Pro Plan",
Checked: true,
})
func RadioGroup ¶
func RadioGroup(props RadioGroupProps) templ.Component
RadioGroup renders a fieldset of radio buttons with legend, error, and help text
@forms.RadioGroup(forms.RadioGroupProps{
Name: "plan",
Label: "Select a plan",
Options: []forms.RadioOption{
{Value: "free", Label: "Free"},
{Value: "pro", Label: "Pro"},
},
})
func SanitizeID ¶
SanitizeID returns a safe HTML ID from arbitrary text
func Select ¶
func Select(props SelectProps) templ.Component
Select renders a labeled select dropdown
@forms.Select(forms.SelectProps{
Name: "country",
Label: "Country",
Options: []forms.SelectOption{
{Value: "de", Label: "Germany"},
{Value: "at", Label: "Austria"},
},
})
Example ¶
_ = Select(SelectProps{
Name: "country",
Label: "Country",
Options: []SelectOption{
{Value: "us", Label: "United States"},
{Value: "de", Label: "Germany"},
},
})
func Textarea ¶
func Textarea(props TextareaProps) templ.Component
Textarea renders a labeled textarea
@forms.Textarea(forms.TextareaProps{
Name: "bio",
Label: "Bio",
Rows: 4,
Placeholder: "Tell us about yourself...",
})
Example ¶
_ = Textarea(TextareaProps{
Name: "bio",
Label: "Biography",
Placeholder: "Tell us about yourself...",
Rows: 4,
MaxLength: 500,
})
func Toggle ¶
func Toggle(props ToggleProps) templ.Component
Toggle renders an accessible toggle switch Uses a visually-hidden checkbox for native form submission and accessibility.
@forms.Toggle(forms.ToggleProps{Name: "notifications", Label: "Enable notifications", Checked: true})
func ValidationSummary ¶ added in v0.3.0
func ValidationSummary(props ValidationSummaryProps) templ.Component
ValidationSummary renders an accessible error summary for form validation
@forms.ValidationSummary(forms.ValidationSummaryProps{
Errors: []forms.ValidationError{
{Field: "email", Message: "Email is required"},
{Field: "name", Message: "Name must be at least 2 characters"},
},
})
Types ¶
type CheckboxProps ¶
type CheckboxProps struct {
utils.BaseProps
Name string
Value string
Label string
Checked bool
Required bool
Disabled bool
Error string
HelpText string
}
CheckboxProps configures a checkbox input
func DefaultCheckboxProps ¶
func DefaultCheckboxProps() CheckboxProps
DefaultCheckboxProps returns sensible defaults
type ComboboxOption ¶ added in v0.4.0
ComboboxOption represents a single selectable option in a combobox.
type ComboboxProps ¶ added in v0.4.0
type ComboboxProps struct {
utils.BaseProps
Name string
Label string
Placeholder string
Options []ComboboxOption
Value string
Required bool
Disabled bool
Error string
HelpText string
}
ComboboxProps configures an autocomplete text input with a filterable dropdown of options. The selected value is submitted via a hidden input with the given Name, so it works in plain HTML forms and HTMX requests.
func DefaultComboboxProps ¶ added in v0.4.0
func DefaultComboboxProps() ComboboxProps
DefaultComboboxProps returns sensible defaults
type DatePickerProps ¶ added in v0.4.0
type DatePickerProps struct {
utils.BaseProps
Name string
Value string
Label string
Min string
Max string
Required bool
Disabled bool
Error string
HelpText string
}
DatePickerProps configures a native HTML date input
func DefaultDatePickerProps ¶ added in v0.4.0
func DefaultDatePickerProps() DatePickerProps
DefaultDatePickerProps returns sensible defaults
type FileInputProps ¶
type FileInputProps struct {
utils.BaseProps
Name string
Label string
Required bool
Disabled bool
Error string
HelpText string
Accept string // e.g. ".jpg,.png" or "image/*"
Multiple bool
}
FileInputProps configures a file input
func DefaultFileInputProps ¶
func DefaultFileInputProps() FileInputProps
DefaultFileInputProps returns sensible defaults
type FormFieldProps ¶ added in v0.6.0
FormFieldProps configures the standard form field wrapper structure. Used by FormFieldWrapper to DRY up all form control components.
type FormMethod ¶
type FormMethod string
FormMethod defines the HTTP method for a form. HTML forms only support GET and POST (per the HTML spec). For HTMX requests using other verbs (PUT, DELETE, PATCH), set Method to FormPost and add the appropriate hx-* attribute via Attrs:
forms.FormProps{
Method: forms.FormPost,
Attrs: templ.Attributes{"hx-put": "/api/item/123"},
}
const ( FormGet FormMethod = "GET" FormPost FormMethod = "POST" )
type FormProps ¶
type FormProps struct {
utils.BaseProps
Action string
Method FormMethod
CSRFToken string
}
FormProps configures a form wrapper
func DefaultFormProps ¶
func DefaultFormProps() FormProps
DefaultFormProps returns sensible defaults
type InputGroupProps ¶
type InputGroupProps struct {
utils.BaseProps
LeftAddon templ.Component
RightAddon templ.Component
}
InputGroupProps configures an input group with optional left/right addons
Use with Input, Select, or Textarea. When using an addon, add padding to the input so the content doesn't overlap:
- Left addon → add "pl-10" to input Class
- Right addon → add "pr-10" to input Class
- Both → add "pl-10 pr-10"
func DefaultInputGroupProps ¶
func DefaultInputGroupProps() InputGroupProps
DefaultInputGroupProps returns sensible defaults
type InputProps ¶
type InputProps struct {
utils.BaseProps
Type InputType
Name string
Value string
Placeholder string
Label string
Required bool
Disabled bool
ReadOnly bool
AutoFocus bool
MaxLength int
Error string
HelpText string
}
InputProps configures a text input
func DefaultInputProps ¶
func DefaultInputProps() InputProps
DefaultInputProps returns sensible defaults
type InputType ¶
type InputType string
InputType defines HTML input types
const ( InputText InputType = "text" InputEmail InputType = "email" InputPassword InputType = "password" InputNumber InputType = "number" InputTel InputType = "tel" InputURL InputType = "url" InputDate InputType = "date" InputTime InputType = "time" InputDatetime InputType = "datetime-local" InputSearch InputType = "search" InputHidden InputType = "hidden" )
type RadioGroupProps ¶
type RadioGroupProps struct {
utils.BaseProps
Name string
Label string // renders as <legend>
Options []RadioOption
Inline bool // horizontal layout
Error string // group-level error
HelpText string
Required bool
}
RadioGroupProps configures a group of radio buttons
func DefaultRadioGroupProps ¶
func DefaultRadioGroupProps() RadioGroupProps
DefaultRadioGroupProps returns sensible defaults
type RadioOption ¶
RadioOption represents a single option in a radio group
type RadioProps ¶
type RadioProps struct {
utils.BaseProps
Name string
Value string
Label string
Checked bool
Disabled bool
}
RadioProps configures a single radio button input
func DefaultRadioProps ¶
func DefaultRadioProps() RadioProps
DefaultRadioProps returns sensible defaults
type SelectOption ¶
SelectOption represents a single option in a select. If both Disabled and Selected are true, Selected takes precedence (Disabled is cleared) because a disabled option cannot be meaningfully pre-selected by the user.
type SelectProps ¶
type SelectProps struct {
utils.BaseProps
Name string
Label string
Options []SelectOption
Required bool
Disabled bool
Error string
HelpText string
}
SelectProps configures a select dropdown
func DefaultSelectProps ¶
func DefaultSelectProps() SelectProps
DefaultSelectProps returns sensible defaults
type TextareaProps ¶
type TextareaProps struct {
utils.BaseProps
Name string
Value string
Placeholder string
Label string
Rows int
Required bool
Disabled bool
ReadOnly bool
MaxLength int
Error string
HelpText string
}
TextareaProps configures a textarea
func DefaultTextareaProps ¶
func DefaultTextareaProps() TextareaProps
DefaultTextareaProps returns sensible defaults
type ToggleProps ¶
type ToggleProps struct {
utils.BaseProps
Name string
Label string
Checked bool
Disabled bool
Required bool
Error string
HelpText string
Size ToggleSize
}
ToggleProps configures a toggle/switch input
func DefaultToggleProps ¶
func DefaultToggleProps() ToggleProps
DefaultToggleProps returns sensible defaults
type ToggleSize ¶
type ToggleSize string
ToggleSize defines the size of a toggle switch
const ( ToggleSizeSM ToggleSize = "sm" ToggleSizeMD ToggleSize = "md" ToggleSizeLG ToggleSize = "lg" )
type ValidationError ¶ added in v0.3.0
FieldError represents a single field validation error
type ValidationSummaryProps ¶ added in v0.3.0
type ValidationSummaryProps struct {
utils.BaseProps
Errors []ValidationError
}
ValidationSummaryProps configures a form validation error summary
func DefaultValidationSummaryProps ¶ added in v0.3.0
func DefaultValidationSummaryProps() ValidationSummaryProps
DefaultValidationSummaryProps returns sensible defaults