Documentation
¶
Overview ¶
Package form provides form wrapper and validation helpers.
Index ¶
- func Description(text string, opts ...DescriptionOption) g.Node
- func Error(text string, opts ...ErrorOption) g.Node
- func Field(labelText string, control g.Node, opts ...FieldOption) g.Node
- func FieldControl(control g.Node) g.Node
- func FieldDescription(text string) g.Node
- func FieldError(text string) g.Node
- func FieldLabel(text, htmlFor string, required bool) g.Node
- func Form(opts []Option, children ...g.Node) g.Node
- func NewValidationError(field, message string) error
- func Validate(value string, validators ...Validator) error
- func ValidateField(field, value string, validators ...Validator) error
- type DescriptionOption
- type DescriptionProps
- type ErrorOption
- type ErrorProps
- type FieldOption
- type FieldProps
- type Option
- type Props
- type ValidationError
- type Validator
- func Alpha() Validator
- func AlphaNumeric() Validator
- func Combine(validators ...Validator) Validator
- func Email() Validator
- func In(allowed []string) Validator
- func MaxLength(maxLen int) Validator
- func MinLength(minLen int) Validator
- func Numeric() Validator
- func Pattern(pattern, message string) Validator
- func Required() Validator
- func RequiredWithMessage(message string) Validator
- func URL() Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Description ¶
func Description(text string, opts ...DescriptionOption) g.Node
Description creates a standalone helper text component
Example:
desc := form.Description(
"Enter your email address for password reset",
form.WithDescriptionClass("mt-2"),
)
func Error ¶
func Error(text string, opts ...ErrorOption) g.Node
Error creates a standalone error message component
Example:
err := form.Error(
"Invalid email address",
form.WithErrorClass("mt-2"),
)
func Field ¶
Field creates a complete form field with label, control, error, and description
Example:
field := form.Field(
"Email",
input.Input(
input.WithType("email"),
input.WithPlaceholder("you@example.com"),
),
form.WithID("email"),
form.WithRequired(),
form.WithDescription("We'll never share your email."),
)
func FieldControl ¶
FieldControl is a simple wrapper for form controls (for API consistency)
func FieldDescription ¶
FieldDescription creates a form field description/helper text
func FieldLabel ¶
FieldLabel creates a form field label
func NewValidationError ¶
NewValidationError creates a new validation error
func ValidateField ¶
ValidateField validates a field with a name for better error messages
Types ¶
type DescriptionOption ¶
type DescriptionOption func(*DescriptionProps)
DescriptionOption is a functional option for configuring descriptions
func WithDescriptionAttrs ¶
func WithDescriptionAttrs(attrs ...g.Node) DescriptionOption
WithDescriptionAttrs adds custom attributes
func WithDescriptionClass ¶
func WithDescriptionClass(class string) DescriptionOption
WithDescriptionClass adds custom classes
type DescriptionProps ¶
DescriptionProps defines description configuration
type ErrorOption ¶
type ErrorOption func(*ErrorProps)
ErrorOption is a functional option for configuring error messages
func WithErrorAttrs ¶
func WithErrorAttrs(attrs ...g.Node) ErrorOption
WithErrorAttrs adds custom attributes
func WithErrorClass ¶
func WithErrorClass(class string) ErrorOption
WithErrorClass adds custom classes
type ErrorProps ¶
ErrorProps defines error message configuration
type FieldOption ¶
type FieldOption func(*FieldProps)
FieldOption is a functional option for configuring form fields
func WithDescription ¶
func WithDescription(desc string) FieldOption
WithDescription sets helper text
func WithFieldClass ¶
func WithFieldClass(class string) FieldOption
WithFieldClass adds custom classes
type FieldProps ¶
type FieldProps struct {
ID string
Name string
Required bool
Disabled bool
Error string
Description string
Class string
}
FieldProps defines form field configuration
type ValidationError ¶
ValidationError represents a validation error
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
type Validator ¶
Validator is a function that validates a value and returns an error if invalid
func Alpha ¶
func Alpha() Validator
Alpha validates that a value contains only alphabetic characters
Example:
validator := form.Alpha()
if err := validator("abc"); err != nil {
// Handle error: "Must contain only letters"
}
func AlphaNumeric ¶
func AlphaNumeric() Validator
AlphaNumeric validates that a value contains only alphanumeric characters
Example:
validator := form.AlphaNumeric()
if err := validator("abc123"); err != nil {
// Handle error: "Must contain only letters and numbers"
}
func Combine ¶
Combine combines multiple validators into one
Example:
validator := form.Combine(
form.Required(),
form.Email(),
form.MaxLength(100),
)
if err := validator("user@example.com"); err != nil {
// Handle first validation error
}
func Email ¶
func Email() Validator
Email validates that a value is a valid email address
Example:
validator := form.Email()
if err := validator("invalid-email"); err != nil {
// Handle error: "Invalid email address"
}
func In ¶
In validates that a value is in the specified list
Example:
validator := form.In([]string{"small", "medium", "large"})
if err := validator("xlarge"); err != nil {
// Handle error: "Must be one of: small, medium, large"
}
func MaxLength ¶
MaxLength validates that a value does not exceed the specified length
Example:
validator := form.MaxLength(100)
if err := validator("very long text..."); err != nil {
// Handle error: "Must not exceed 100 characters"
}
func MinLength ¶
MinLength validates that a value has at least the specified length
Example:
validator := form.MinLength(8)
if err := validator("short"); err != nil {
// Handle error: "Must be at least 8 characters"
}
func Numeric ¶
func Numeric() Validator
Numeric validates that a value contains only numeric characters
Example:
validator := form.Numeric()
if err := validator("12345"); err != nil {
// Handle error: "Must contain only numbers"
}
func Pattern ¶
Pattern validates that a value matches the specified regex pattern
Example:
validator := form.Pattern(`^\d{3}-\d{3}-\d{4}$`, "Invalid phone number format")
if err := validator("123-456-7890"); err != nil {
// Handle error
}
func Required ¶
func Required() Validator
Required validates that a value is not empty
Example:
validator := form.Required()
if err := validator(""); err != nil {
// Handle error: "This field is required"
}
func RequiredWithMessage ¶
RequiredWithMessage validates that a value is not empty with a custom message