Documentation
¶
Overview ¶
Package forms provides form state management and validation.
Index ¶
- type AsyncFormValidator
- type AsyncValidator
- type Builder
- func (b *Builder) AsyncValidator(validator AsyncFormValidator) *Builder
- func (b *Builder) Build() (*Form, []FieldSpec)
- func (b *Builder) Checkbox(name, label string, initial bool, validators ...Validator) *Builder
- func (b *Builder) Date(name, label string, initial any, validators ...Validator) *Builder
- func (b *Builder) Email(name, label, initial string, validators ...Validator) *Builder
- func (b *Builder) Field(spec FieldSpec) *Builder
- func (b *Builder) MultiSelect(name, label string, options []string, initial []string, ...) *Builder
- func (b *Builder) Number(name, label string, initial float64, validators ...Validator) *Builder
- func (b *Builder) Password(name, label, initial string, validators ...Validator) *Builder
- func (b *Builder) Select(name, label string, options []string, initial string, validators ...Validator) *Builder
- func (b *Builder) Text(name, label, initial string, validators ...Validator) *Builder
- func (b *Builder) Time(name, label string, initial any, validators ...Validator) *Builder
- func (b *Builder) Validator(validator FormValidator) *Builder
- type Field
- type FieldBase
- func (f *FieldBase) AsyncValidators() []AsyncValidator
- func (f *FieldBase) Dirty() bool
- func (f *FieldBase) Errors() []string
- func (f *FieldBase) MarkTouched()
- func (f *FieldBase) Name() string
- func (f *FieldBase) ResetState()
- func (f *FieldBase) SetAsyncValidators(validators ...AsyncValidator)
- func (f *FieldBase) SetErrors(messages []string)
- func (f *FieldBase) SetInitial(value any)
- func (f *FieldBase) SetValidators(validators ...Validator)
- func (f *FieldBase) Touched() bool
- func (f *FieldBase) UpdateDirty(value any)
- func (f *FieldBase) Valid() bool
- func (f *FieldBase) ValidateValue(value any) []ValidationError
- func (f *FieldBase) Validating() bool
- func (f *FieldBase) ValidatingSignal() *state.Signal[bool]
- type FieldSpec
- type FieldType
- type Form
- func (f *Form) AddAsyncValidator(validator AsyncFormValidator)
- func (f *Form) AddField(field Field)
- func (f *Form) AddValidator(validator FormValidator)
- func (f *Form) Cancel()
- func (f *Form) DirtySignal() *state.Signal[bool]
- func (f *Form) Field(name string) Field
- func (f *Form) Get(name string) any
- func (f *Form) OnCancel(fn func())
- func (f *Form) OnSubmit(fn func(values Values))
- func (f *Form) Reset()
- func (f *Form) Set(name string, value any)
- func (f *Form) SetDependencies(field string, deps ...string)
- func (f *Form) Submit()
- func (f *Form) SubmitAsync(ctx context.Context) <-chan []ValidationError
- func (f *Form) SubmittingSignal() *state.Signal[bool]
- func (f *Form) ValidSignal() *state.Signal[bool]
- func (f *Form) Validate() []ValidationError
- func (f *Form) ValidateAsync(ctx context.Context) <-chan []ValidationError
- func (f *Form) ValidatingSignal() *state.Signal[bool]
- func (f *Form) Values() Values
- type FormValidator
- type SimpleField
- type ValidationError
- type Validator
- func All(validators ...Validator) Validator
- func AlphaNumeric(msg string) Validator
- func And(validators ...Validator) Validator
- func Any(validators ...Validator) Validator
- func ConditionalRequired(condition func() bool, msg string) Validator
- func Custom(fn func(any) *ValidationError) Validator
- func Email(msg string) Validator
- func JSON() Validator
- func Length(n int, msg string) Validator
- func MatchField(other Field, msg string) Validator
- func Max(n float64, msg string) Validator
- func MaxLength(n int, msg string) Validator
- func Min(n float64, msg string) Validator
- func MinLength(n int, msg string) Validator
- func Not(v Validator, msg string) Validator
- func Numeric(msg string) Validator
- func OneOf(values ...string) Validator
- func Or(validators ...Validator) Validator
- func Pattern(regex string, msg string) Validator
- func Range(min, max float64, msg string) Validator
- func Required(msg string) Validator
- func URL(msg string) Validator
- func UUID() Validator
- func When(cond func() bool, v Validator) Validator
- type Values
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AsyncFormValidator ¶
type AsyncFormValidator interface {
ValidateAsync(ctx context.Context, values Values) []ValidationError
}
AsyncFormValidator validates multiple fields asynchronously.
func AsyncForm ¶
func AsyncForm(fn func(ctx context.Context, values Values) []ValidationError) AsyncFormValidator
AsyncForm wraps a function into an AsyncFormValidator.
type AsyncValidator ¶
type AsyncValidator interface {
ValidateAsync(ctx context.Context, value any) *ValidationError
}
AsyncValidator validates a field value asynchronously.
func Async ¶
func Async(fn func(ctx context.Context, value any) *ValidationError) AsyncValidator
Async wraps a function into an AsyncValidator.
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder constructs forms using a fluent DSL.
func (*Builder) AsyncValidator ¶
func (b *Builder) AsyncValidator(validator AsyncFormValidator) *Builder
AsyncValidator adds a form-level async validator.
func (*Builder) MultiSelect ¶
func (b *Builder) MultiSelect(name, label string, options []string, initial []string, validators ...Validator) *Builder
MultiSelect adds a multi-select field.
func (*Builder) Select ¶
func (b *Builder) Select(name, label string, options []string, initial string, validators ...Validator) *Builder
Select adds a single-choice select field.
func (*Builder) Validator ¶
func (b *Builder) Validator(validator FormValidator) *Builder
Validator adds a form-level validator.
type Field ¶
type Field interface {
Name() string
Value() any
SetValue(any)
Dirty() bool
Touched() bool
Valid() bool
Errors() []string
Validate() []ValidationError
Reset()
}
Field represents a form field.
type FieldBase ¶
type FieldBase struct {
// contains filtered or unexported fields
}
FieldBase provides common field state management.
func NewFieldBase ¶
NewFieldBase constructs a field base.
func (*FieldBase) AsyncValidators ¶
func (f *FieldBase) AsyncValidators() []AsyncValidator
AsyncValidators returns a copy of async validators.
func (*FieldBase) MarkTouched ¶
func (f *FieldBase) MarkTouched()
MarkTouched marks the field as touched.
func (*FieldBase) ResetState ¶
func (f *FieldBase) ResetState()
ResetState clears dirty/touched/errors.
func (*FieldBase) SetAsyncValidators ¶
func (f *FieldBase) SetAsyncValidators(validators ...AsyncValidator)
SetAsyncValidators updates async validators.
func (*FieldBase) SetInitial ¶
SetInitial updates the initial value used for dirty tracking.
func (*FieldBase) SetValidators ¶
SetValidators updates the validators.
func (*FieldBase) UpdateDirty ¶
UpdateDirty updates dirty state based on the provided value.
func (*FieldBase) ValidateValue ¶
func (f *FieldBase) ValidateValue(value any) []ValidationError
ValidateValue validates a value using the field validators.
func (*FieldBase) Validating ¶
Validating reports whether async validation is running.
type FieldSpec ¶
type FieldSpec struct {
Name string
Label string
Type FieldType
Placeholder string
Options []string
Initial any
Validators []Validator
AsyncValidators []AsyncValidator
DependsOn []string
}
FieldSpec describes a form field for builders and renderers.
type FieldType ¶
type FieldType string
FieldType identifies a form field type.
const ( FieldText FieldType = "text" FieldNumber FieldType = "number" FieldEmail FieldType = "email" FieldPassword FieldType = "password" FieldCheckbox FieldType = "checkbox" FieldSelect FieldType = "select" FieldMultiSelect FieldType = "multiselect" FieldDate FieldType = "date" FieldTime FieldType = "time" )
type Form ¶
type Form struct {
// contains filtered or unexported fields
}
Form manages a collection of fields.
func (*Form) AddAsyncValidator ¶
func (f *Form) AddAsyncValidator(validator AsyncFormValidator)
AddAsyncValidator registers an async cross-field validator.
func (*Form) AddValidator ¶
func (f *Form) AddValidator(validator FormValidator)
AddValidator registers a cross-field validator.
func (*Form) DirtySignal ¶
DirtySignal returns the form dirty signal.
func (*Form) SetDependencies ¶
SetDependencies declares that a field depends on other fields.
func (*Form) SubmitAsync ¶
func (f *Form) SubmitAsync(ctx context.Context) <-chan []ValidationError
SubmitAsync runs validation asynchronously and invokes onSubmit if valid.
func (*Form) SubmittingSignal ¶
SubmittingSignal returns the form submitting signal.
func (*Form) ValidSignal ¶
ValidSignal returns the form valid signal.
func (*Form) Validate ¶
func (f *Form) Validate() []ValidationError
Validate validates all fields and form validators.
func (*Form) ValidateAsync ¶
func (f *Form) ValidateAsync(ctx context.Context) <-chan []ValidationError
ValidateAsync validates fields and async validators, returning a channel of errors.
func (*Form) ValidatingSignal ¶
ValidatingSignal returns the async validating signal.
type FormValidator ¶
type FormValidator interface {
Validate(values Values) []ValidationError
}
FormValidator validates multiple fields at once.
func FieldsMatch ¶
func FieldsMatch(a, b, msg string) FormValidator
FieldsMatch validates that two fields are equal.
type SimpleField ¶
type SimpleField struct {
*FieldBase
// contains filtered or unexported fields
}
SimpleField is a basic field implementation storing its own value.
func NewField ¶
func NewField(name string, initial any, validators ...Validator) *SimpleField
NewField creates a new simple field.
func (*SimpleField) SetValue ¶
func (f *SimpleField) SetValue(value any)
SetValue updates the value and dirty state.
func (*SimpleField) Validate ¶
func (f *SimpleField) Validate() []ValidationError
Validate runs validation on the current value.
func (*SimpleField) ValidateAsync ¶
func (f *SimpleField) ValidateAsync(ctx context.Context) <-chan []ValidationError
ValidateAsync runs async validators and updates errors when complete.
type ValidationError ¶
ValidationError describes a validation failure.
type Validator ¶
type Validator interface {
Validate(value any) *ValidationError
}
Validator validates a field value.
func AlphaNumeric ¶
AlphaNumeric ensures the value contains only letters and digits.
func ConditionalRequired ¶
ConditionalRequired requires a non-empty value only when the condition returns true.
func Custom ¶
func Custom(fn func(any) *ValidationError) Validator
Custom wraps a validator function.
func MatchField ¶
MatchField validates that this field's value matches another field's current value. It reads the other field's value at validation time via other.Value().