Malloy
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | List of all members
malloy::html::form Class Reference

#include <form.hpp>

Public Types

enum class  encoding { url , multipart , plain }
 

Public Member Functions

 form (http::method method, std::string action, encoding enc)
 
virtual ~form ()=default
 
http::method method () const noexcept
 
std::string_view action () const noexcept
 
encoding get_encoding () const noexcept
 
std::string_view encoding_to_string () const
 
std::vector< form_fieldfields () const noexcept
 
bool add_field (form_field &&field)
 
bool has_field (std::string_view field_name) const
 
void populate (const form_data &data)
 
void clear_values ()
 

Static Public Member Functions

static std::string_view encoding_to_string (encoding enc)
 
static std::optional< encodingencoding_from_string (std::string_view str)
 
static std::optional< form_dataparse (const malloy::http::request<> &req)
 

Detailed Description

Class for handling HTML forms.

This class supports all three encoding types supported by HTML5:

Member Enumeration Documentation

◆ encoding

enum class malloy::html::form::encoding
strong

Encoding type.

Constructor & Destructor Documentation

◆ form()

form::form ( http::method  method,
std::string  action,
encoding  enc 
)

Constructor.

Parameters
methodThe HTTP method.
actionthe Action (eg. target link).
encThe encoding type.

◆ ~form()

virtual malloy::html::form::~form ( )
virtualdefault

Destructor.

Member Function Documentation

◆ action()

std::string_view malloy::html::form::action ( ) const
inlinenoexcept

Get the action string.

Returns
The action string.

◆ add_field()

bool form::add_field ( form_field &&  field)

Adds a field to the form.

Note
This will fail if the supplied field has an invalid name.
This will fail if a field with the same name exists already.
Parameters
fieldThe field.
Returns
Whether the field was added.
Here is the call graph for this function:

◆ clear_values()

void form::clear_values ( )

Clears the pre-population values of all fields.

◆ encoding_from_string()

std::optional< form::encoding > form::encoding_from_string ( std::string_view  str)
static

Returns the econding type corresponding to the provided MIME type/

Parameters
strThe MIME type (eg. "application/x-www-form-urlencoded").
Returns
The corresponding encoding type (if any)

◆ encoding_to_string() [1/2]

std::string_view malloy::html::form::encoding_to_string ( ) const
inline

Returns the MIME type corresponding to the encoding type.

Returns
The MIME type (eg. "application/x-www-form-urlencoded").
Here is the call graph for this function:

◆ encoding_to_string() [2/2]

std::string_view form::encoding_to_string ( encoding  enc)
static

Returns the MIME type corresponding to the encoding type.

Returns
The MIME type (eg. "application/x-www-form-urlencoded").

◆ fields()

std::vector< form_field > malloy::html::form::fields ( ) const
inlinenoexcept

Get the form fields.

Returns
The form fields.

◆ get_encoding()

encoding malloy::html::form::get_encoding ( ) const
inlinenoexcept

Get encoding type.

Returns
The encoding type.

◆ has_field()

bool form::has_field ( std::string_view  field_name) const

Checks whether a field with a specific name already exists.

Parameters
field_nameThe field name.
Returns
Whether a field with the specified name already exists.

◆ method()

http::method malloy::html::form::method ( ) const
inlinenoexcept

Get the method.

Returns
The method.

◆ parse()

std::optional< form_data > form::parse ( const malloy::http::request<> &  req)
static

Parse a request matching this form.

Note
If the request doesn't provide the corresponding Content-Type field parsing will not be attempted.
Parameters
reqThe request.
Returns
The parsed form data (if any).
Here is the call graph for this function:

◆ populate()

void form::populate ( const form_data data)

This will update each field's value member with the content member of the parsed data;

This function is useful to call if serving the same form back to the user after parsing to pre-populate form fields so the user doesn't have to re-enter everything.

Note
This will skip/ignore any fields of HTML type 'password'.
This will skip/ignore any fields of HTML type 'file'.
Parameters
dataThe data.
Here is the call graph for this function:

The documentation for this class was generated from the following files: