forms

package
v0.0.0-...-3a609b8 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 7 Imported by: 1 module, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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")
	// ErrFileTooLarge can be used to indicate a file upload had a size greater than a set limit.
	ErrFileTooLarge = errors.New("file is too large")
)

Functions

func DecodeForm

func DecodeForm[T FormInput](req *http.Request) (T, 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 DecodeMultiPartForm

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

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-nil error is returned.

Types

type FileUpload

type FileUpload struct {
	*multipart.FileHeader `json:"header" validate:"required"`
	multipart.File        `json:"data"   validate:"required"`
}

func DecodeMultipartFile

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

DecodeMultipartFile will decode the file represented by the given field in a multipart form submission. It will return the data as a FileUpload. If decoding fails, a non-nil error is returned.

func (FileUpload) GetFilename

func (f FileUpload) GetFilename() string

func (FileUpload) GetHeader

func (f FileUpload) GetHeader(key string) string

func (FileUpload) GetSize

func (f FileUpload) GetSize() int64

GetSize returns the file size.

func (FileUpload) ParseMimetype

func (f FileUpload) ParseMimetype() (string, error)

ParseMimetype attempts to parse and return the mime type of the file from its mime header.

func (FileUpload) Validate

func (f FileUpload) Validate() error

type FormInput

type FormInput interface {
	Validate() 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