pub trait Form: Sized {
type Context: FormContext + Send;
// Required methods
fn from_request<'life0, 'async_trait>(
request: &'life0 mut Request,
) -> Pin<Box<dyn Future<Output = Result<FormResult<Self>, FormError>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn to_context<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Self::Context> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
// Provided method
fn build_context<'life0, 'async_trait>(
request: &'life0 mut Request,
) -> Pin<Box<dyn Future<Output = Result<Self::Context, FormError>> + Send + 'async_trait>>
where Self: Send + 'async_trait,
'life0: 'async_trait { ... }
}Expand description
A trait for types that can be used as forms.
This trait is used to define a type that can be used as a form. It provides a way to create a form from a request, build a context from the request, and validate the form.
§Deriving
This trait can, and should be derived using the Form derive
macro. This macro generates the implementation of the trait for the type,
including the implementation of the FormContext trait for the context
type.
use cot::form::Form;
#[derive(Form)]
struct MyForm {
#[form(opts(max_length = 100))]
name: String,
}Required Associated Types§
Sourcetype Context: FormContext + Send
type Context: FormContext + Send
The context type associated with the form.
Required Methods§
Sourcefn from_request<'life0, 'async_trait>(
request: &'life0 mut Request,
) -> Pin<Box<dyn Future<Output = Result<FormResult<Self>, FormError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn from_request<'life0, 'async_trait>(
request: &'life0 mut Request,
) -> Pin<Box<dyn Future<Output = Result<FormResult<Self>, FormError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Creates a form struct from a request.
§Errors
This method should return an error if the form data could not be read from the request.
Sourcefn to_context<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Self::Context> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn to_context<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Self::Context> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Creates the context for the form from self.
This is useful for pre-populating forms with objects created in the code or obtained externally, such as from a database.
Provided Methods§
Sourcefn build_context<'life0, 'async_trait>(
request: &'life0 mut Request,
) -> Pin<Box<dyn Future<Output = Result<Self::Context, FormError>> + Send + 'async_trait>>where
Self: Send + 'async_trait,
'life0: 'async_trait,
fn build_context<'life0, 'async_trait>(
request: &'life0 mut Request,
) -> Pin<Box<dyn Future<Output = Result<Self::Context, FormError>> + Send + 'async_trait>>where
Self: Send + 'async_trait,
'life0: 'async_trait,
Builds the context for the form from a request.
Note that this doesn’t try to convert the values from the form fields
into the final types, so this context object may not include all the
errors. The conversion is done in the Self::from_request method.
§Errors
This method should return an error if the form data could not be read from the request.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.