form

package
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2024 License: MIT, MIT Imports: 16 Imported by: 9

README

Creamsensation - Form

Example code

package example

import . "github.com/creamsensation/form"

type ExampleForm struct {
  Form
  Name  Field[string] 
  Age   Field[int]
}

func createExampleForm() (ExampleForm, error) {
  form := New(
    Add("name").With(Text(), Validate.Required()),
    Add("age").With(Number[int](), Validate.Required()),
  )
  return Build[ExampleForm](form)
}

New()

Creates new form builder, which accept field builders

New(fields...)
Builder - Action()

Set form action

formBuilder.Action(action)
Builder - Add()

Same as Add() function, only alternative

formBuilder.Add(name)
Builder - Get()

Get form field

formBuilder.Get(name)
Builder - Limit()

Data limit (MBs)

formBuilder.Limit(limit)
Builder - Method()

Set form method

formBuilder.Method(method)
Builder - Name()

Set form name

formBuilder.Name(name)
Builder - Request()

Provide request to form, it uses native *http.Request

formBuilder.Request(request)

Add()

Creates new field builder

Add(config, validators...)

Validate

Validate - Required()

Use when form field value is required, it works with string, int, floats, bool and Multipart

Validate.Required()
--
Add("example").With(Text(), Validate.Required())
Validate - Min()

Use when form field value must have minimal value or minimal length, it works with string, int, floats

Validate.Min(1)
--
Add("text").With(Text(), Validate.Min(1))
Add("amount").With(Number[float64](), Validate.Min(1))
Validate - Max()

Use when form field value must have maximum value or maximum length, it works with string, int, floats

Validate.Max(10)
--
Add("text").With(Text(), Validate.Max(10))
Add("amount").With(Number[float64](), Validate.Max(10))
Validate - Email()

Use when form field value must have email pattern, it works with string

Validate.Email()
--
Add("email").With(Email("test@test.cz"), Validate.Email())

Build()

Creates form from form builder, you have to provide result type

Build[ExampleForm](formBuilder)

CreateStruct()

Convert form struct to any data model struct, you have to provide source and result type

CreateStruct[ExampleForm, Model](&form)

Documentation

Index

Constants

View Source
const (
	CsrfName  = "__csrf_name__"
	CsrfToken = "__csrf_token__"
)
View Source
const (
	DefaultBodyLimit = 256
)

Variables

View Source
var Validate = Validators{}

Functions

func Build

func Build[T any](b *Builder) (T, error)

func CreateStruct

func CreateStruct[S, R any](src *S) R

func CreateValidator

func CreateValidator[T any](pattern string) func(value ...T) Validator

func Csrf

func Csrf(name, token string) gox.Node

func MustBuild

func MustBuild[T any](b *Builder) T

func ParseForm

func ParseForm(req *http.Request, limit int) (int, error)

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

func New

func New(fields ...*FieldBuilder) *Builder

func (*Builder) Action

func (b *Builder) Action(action string) *Builder

func (*Builder) Add

func (b *Builder) Add(name string) *FieldBuilder

func (*Builder) Csrf

func (b *Builder) Csrf(name, token string) *Builder

func (*Builder) Get

func (b *Builder) Get(name string) *FieldBuilder

func (*Builder) Hx

func (b *Builder) Hx() *Builder

func (*Builder) Limit

func (b *Builder) Limit(limit int) *Builder

func (*Builder) Messages

func (b *Builder) Messages(messages Messages) *Builder

func (*Builder) Method

func (b *Builder) Method(method string) *Builder

func (*Builder) Name

func (b *Builder) Name(name string) *Builder

func (*Builder) Request

func (b *Builder) Request(request *http.Request) *Builder

func (*Builder) State added in v0.1.6

func (b *Builder) State(state state) *Builder

type Config

type Config struct {
	// MBs
	Limit int `json:"limit"`
}

type Field

type Field[T any] struct {
	Type      string
	DataType  string
	Id        string
	Name      string
	Label     string
	Text      string
	Value     T
	Messages  []string
	Autofocus bool
	Disabled  bool
	Required  bool
	Multiple  bool
}

type FieldBuilder

type FieldBuilder struct {
	// contains filtered or unexported fields
}

func Add

func Add(name string) *FieldBuilder

func (*FieldBuilder) Autofocus

func (b *FieldBuilder) Autofocus(autofocus ...bool) *FieldBuilder

func (*FieldBuilder) Disabled

func (b *FieldBuilder) Disabled(disabled ...bool) *FieldBuilder

func (*FieldBuilder) Id

func (b *FieldBuilder) Id(id string) *FieldBuilder

func (*FieldBuilder) Label

func (b *FieldBuilder) Label(label string) *FieldBuilder

func (*FieldBuilder) Multiple

func (b *FieldBuilder) Multiple(size ...int) *FieldBuilder

func (*FieldBuilder) Text

func (b *FieldBuilder) Text(text any) *FieldBuilder

func (*FieldBuilder) With

func (b *FieldBuilder) With(config FieldConfig, validators ...Validator) *FieldBuilder

type FieldConfig

type FieldConfig struct {
	// contains filtered or unexported fields
}

func Button

func Button(value ...string) FieldConfig

func Checkbox

func Checkbox(value ...bool) FieldConfig

func Color

func Color(value ...string) FieldConfig

func Date

func Date(value ...time.Time) FieldConfig

func DateTimeLocal

func DateTimeLocal(value ...time.Time) FieldConfig

func Email

func Email(value ...string) FieldConfig

func File

func File(value ...Multipart) FieldConfig

func Hidden

func Hidden[T comparable](value ...T) FieldConfig

func Image

func Image(value ...string) FieldConfig

func Month

func Month(value ...string) FieldConfig

func Number

func Number[T constraints.Float | constraints.Integer](value ...T) FieldConfig

func Password

func Password(value ...string) FieldConfig

func Radio

func Radio(value ...string) FieldConfig

func Range

func Range(value ...string) FieldConfig

func Reset

func Reset(value ...string) FieldConfig
func Search(value ...string) FieldConfig

func Submit

func Submit(value ...string) FieldConfig

func Tel

func Tel(value ...string) FieldConfig

func Text

func Text(value ...string) FieldConfig

func Time

func Time(value ...time.Time) FieldConfig

func Url

func Url(value ...string) FieldConfig

func Week

func Week(value ...string) FieldConfig

type Form

type Form struct {
	Security    security
	Method      string
	ContentType string
	Action      string
	Valid       bool
	Submitted   bool
	Hx          bool
}

func (Form) Csrf

func (f Form) Csrf() gox.Node

func (Form) Node

func (f Form) Node(nodes ...gox.Node) gox.Node

type Messages

type Messages struct {
	Email     string `json:"email" toml:"email" yaml:"email"`
	Required  string `json:"required" toml:"required" yaml:"required"`
	MinText   string `json:"minText" toml:"minText" yaml:"minText"`
	MaxText   string `json:"maxText" toml:"maxText" yaml:"maxText"`
	MinNumber string `json:"minNumber" toml:"minNumber" yaml:"minNumber"`
	MaxNumber string `json:"maxNumber" toml:"maxNumber" yaml:"maxNumber"`
	Multipart string `json:"multipart" toml:"multipart" yaml:"multipart"`
	Invalid   string `json:"invalid" toml:"invalid" yaml:"invalid"`
}

type Multipart

type Multipart struct {
	Key    string `json:"key"`
	Name   string `json:"name"`
	Type   string `json:"type"`
	Suffix string `json:"suffix"`
	Data   []byte `json:"data"`
}

type Validator

type Validator interface{}

type Validators

type Validators struct{}

func (Validators) Email

func (v Validators) Email() Validator

func (Validators) Max

func (v Validators) Max(value int) Validator

func (Validators) Min

func (v Validators) Min(value int) Validator

func (Validators) Required

func (v Validators) Required() Validator

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL