changeset

package
v0.0.0-...-52ea55a Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorTag

func ErrorTag(cs Any, key string) htmltmpl.HTML

ErrorTag renders an error tag if there is an error for the given key in the provided changeset.

func Funcs

func Funcs() htmltmpl.FuncMap

Funcs returns a map of functions that can be used in templates:

  • inputTag: renders an input tag with the given key and value for the provided changeset
  • errorTag: renders an error tag if there is an error for the given key in the provided changeset

func InputTag

func InputTag(cs Any, key string) htmltmpl.HTML

InputTag renders an input tag with the given key and value for the provided changeset.

Types

type Any

type Any interface {
	Value(string) string
	Error(string) error
	AddError(string, error)
	RemoveError(string) error
	HasError(string) bool
}

Any represents any Changeset. It is useful for working with arbitrary changesets outside of generics.

type Changeset

type Changeset[T any] struct {
	Initial url.Values // map of initial values
	Changes url.Values // map of field name that differs from the original value
	Values  url.Values // map of merged changes and original values
	// contains filtered or unexported fields
}

Changeset provides a powerful API for decoding URL values into a struct and validating the struct. It provides a way to check if a given struct is valid and if not a way to access the errors for each field. A changeset is meant to work with HTML form data in concert with the phx-change and phx-submit events.

func New

func New[T any](cc *Config, initial url.Values) *Changeset[T]

New returns a new Changeset of type T using the Config and initilizes the Changeset with the given initial values.

func (*Changeset[T]) AddError

func (c *Changeset[T]) AddError(key string, err error)

AddError adds an error for the given key and marks the field as touched.

func (*Changeset[T]) Error

func (c *Changeset[T]) Error(key string) error

Error returns the error for the given key.

func (*Changeset[T]) Errors

func (c *Changeset[T]) Errors() map[string]error

Errors returns the raw map of errors.

func (*Changeset[T]) HasError

func (c *Changeset[T]) HasError(key string) bool

HasError returns true if Error(key) returns a non-nil error

func (*Changeset[T]) RemoveError

func (c *Changeset[T]) RemoveError(key string) error

RemoveError removes the error for the given key returning the error at the given key or nil if there was no error.

func (*Changeset[T]) Struct

func (c *Changeset[T]) Struct() (*T, error)

Struct returns the changeset Values decoded into a struct of T or an error if there was a problem decoding.

func (*Changeset[T]) Update

func (c *Changeset[T]) Update(newData url.Values, action string) error

Update updates the changeset with new data and action. If action is empty, the changeset will always return true for Valid(). Passing a non-empty action will cause the changeset to run validations which may change the result of Valid() depending on whether or not there are errors and whether or not the field was touched.

func (*Changeset[T]) Valid

func (c *Changeset[T]) Valid() bool

Valid returns true if the changeset is valid or false if it is not. Valid depends on the last action that was performed on the changeset along with the Errors map and whether or not the field was touched. If the action is empty, Valid will always return true. If the action is not empty, Valid will return true if there are no errors or if there are errors but the field was not touched.

func (*Changeset[T]) Value

func (c *Changeset[T]) Value(key string) string

Value returns the value for the given key.

type Config

type Config struct {
	Validator
	Decoder
}

Config is a configuration for a Changeset providing implementations of Validator and Decoder.

type Decoder

type Decoder interface {
	// Decode decodes the url.Values into the struct returning an error if there was a problem.
	Decode(any, url.Values) error
}

Decoder decodes a url.Values into a struct.

type GoPlaygroundChangesetConfig

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

GoPlaygroundChangesetConfig provides a GoPlayground Validate and Decoder based implementation of the Validator and Decoder interfaces.

func NewGoPlaygroundChangesetConfig

func NewGoPlaygroundChangesetConfig() GoPlaygroundChangesetConfig

NewGoPlaygroundChangesetConfig initializes the decoder and configures the validator with translations for the len, lte, and min tags. This is a minimal implementation to show how one can use different decoder and validator libraries with the changeset package.

func (GoPlaygroundChangesetConfig) Decode

func (a GoPlaygroundChangesetConfig) Decode(ptr any, v url.Values) error

Decode decodes the URL values into the struct pointer.

func (GoPlaygroundChangesetConfig) Validate

func (a GoPlaygroundChangesetConfig) Validate(ptr any, vals url.Values) (map[string]error, error)

Validate decodes the URL values into the struct before running the validations on it, returning a map of errors if validation runs successfully or an error if decoding or validation fails.

type Validator

type Validator interface {
	// Validate validates that the provided URL value are valid for the given struct.
	// It returns a map of field name to error message for each field that is invalid
	// or an error if there was a general error validating.
	Validate(any, url.Values) (map[string]error, error)
}

Validator validates url.Values for a give struct and returns a map of field name to error message.

Jump to

Keyboard shortcuts

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