Documentation ¶
Index ¶
- func ErrorTag(cs Any, key string) htmltmpl.HTML
- func Funcs() htmltmpl.FuncMap
- func InputTag(cs Any, key string) htmltmpl.HTML
- type Any
- type Changeset
- func (c *Changeset[T]) AddError(key string, err error)
- func (c *Changeset[T]) Error(key string) error
- func (c *Changeset[T]) Errors() map[string]error
- func (c *Changeset[T]) HasError(key string) bool
- func (c *Changeset[T]) RemoveError(key string) error
- func (c *Changeset[T]) Struct() (*T, error)
- func (c *Changeset[T]) Update(newData url.Values, action string) error
- func (c *Changeset[T]) Valid() bool
- func (c *Changeset[T]) Value(key string) string
- type Config
- type Decoder
- type GoPlaygroundChangesetConfig
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorTag ¶
ErrorTag renders an error tag if there is an error for the given key in 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 ¶
New returns a new Changeset of type T using the Config and initilizes the Changeset with the given initial values.
func (*Changeset[T]) AddError ¶
AddError adds an error for the given key and marks the field as touched.
func (*Changeset[T]) RemoveError ¶
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 ¶
Struct returns the changeset Values decoded into a struct of T or an error if there was a problem decoding.
func (*Changeset[T]) Update ¶
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 ¶
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.
type Config ¶
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.
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.