Documentation
¶
Overview ¶
Package forms contains methods for handling form decoding and encoding.
Index ¶
- Variables
- func DecodeCustom[T FormInput](req *http.Request, decoderFunc func(params url.Values) (T, error)) (T, bool, error)
- func DecodeForm[T FormInput](req *http.Request) (T, bool, error)
- func DecodeMultipartFile(req *http.Request, field string) (*models.FileUpload, error)
- func DecodeMultipartValue(req *http.Request, field string) (string, error)
- func DecodeRequest[T any](req *http.Request) (T, error)
- func EncodeForm[T FormInput](obj T) (url.Values, error)
- type FileUpload
- type FormInput
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
DecodeRequest will decode a request body into the given type.
func EncodeForm ¶
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.