forms

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package forms contains methods for handling form decoding and encoding.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDecode indicates an error occurred during decoding.
	ErrDecode = errors.New("error in decoding")
	// ErrEncode indicates an error occurred during encoding.
	ErrEncode = errors.New("error in encoding")
	// ErrValidation indicates an error occurred during validation.
	ErrValidation = errors.New("validation failed")
	// ErrNoFormData indicates that no form data was parsed.
	ErrNoFormData = errors.New("no form data")
	// ErrSanitise indicates an error occurred during sanitisation.
	ErrSanitise = errors.New("sanitisation failed")
)

Functions

func DecodeCustom

func DecodeCustom[T FormInput](req *http.Request, decoderFunc func(params url.Values) (T, error)) (T, bool, error)

DecodeCustom will decode submitted form contents into the passed in type, using the passed function to decode url.Values into the defined type. It will also will perform validation of the type and will return the type and a boolean true if it is valid. If decoding the form submission fails, a non-nill error is returned.

func DecodeForm

func DecodeForm[T FormInput](req *http.Request) (T, bool, error)

DecodeForm will decode submitted form contents into the passed in type. It will perform validation of the type and will return the type and a boolean true if it is valid. If decoding the form submission fails, a non-nill error is returned.

func DecodeMultipartFile

func DecodeMultipartFile(req *http.Request, field string) (*models.FileUpload, error)

DecodeMultipartFile will the file represented by the given field in a multipart form submission. It will perform validation of the file and will return the file object and a boolean true if it is valid. If decoding fails, a non-nill error is returned.

func DecodeMultipartValue

func DecodeMultipartValue(req *http.Request, field string) (string, error)

DecodeMultipartValue will the file represented by the given field in a multipart form submission. It will perform validation of the file and will return the file object and a boolean true if it is valid. If decoding fails, a non-nill error is returned.

func DecodeRequest

func DecodeRequest[T any](req *http.Request) (T, error)

DecodeRequest will decode a request body into the given type.

func EncodeForm

func EncodeForm[T FormInput](obj T) (url.Values, error)

EncodeForm will encode the given object as url.Values, using the struct tags where possible. It will perform validation of the object before attempting encoding. If the object cannot be encoded or validation fails, a non-nil error is returned.

Types

type FileUpload

type FileUpload interface {
	Set(hdr *multipart.FileHeader, data multipart.File)
}

FileUpload represents file data uploaded through a mutlipart form.

type FormInput

type FormInput interface {
	Valid() error
	Sanitise() error
}

FormInput represents form input data. It has methods to test if the data is valid and to sanitise the input data.

Jump to

Keyboard shortcuts

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