Struct rocket::form::Contextual
source · pub struct Contextual<'v, T> {
pub value: Option<T>,
pub context: Context<'v>,
}
Expand description
An infallible form guard that records form fields and errors during parsing.
This form guard never fails. It should be use only when the form
Context
is required. In all other cases, prefer to use T
directly.
Usage
Contextual
acts as a proxy for any form type, recording all submitted form
values and produced errors and associating them with their corresponding
field name. Contextual
is particularly useful for rendering forms with
previously submitted values and errors associated with form input.
To retrieve the context for a form, use Form<Contextual<'_, T>>
as a data
guard, where T
implements FromForm
. The context
field contains the
form’s Context
:
use rocket::form::{Form, Contextual};
#[post("/submit", data = "<form>")]
fn submit(form: Form<Contextual<'_, T>>) {
if let Some(ref value) = form.value {
// The form parsed successfully. `value` is the `T`.
}
// We can retrieve raw field values and errors.
let raw_id_value = form.context.field_value("id");
let id_errors = form.context.field_errors("id");
}
Context
serializes as a map, so it can be rendered in templates that
require Serialize
types. See the forms guide for further usage details.
Fields§
§value: Option<T>
The value, if it was successfully parsed, or None
otherwise.
context: Context<'v>
The context with all submitted fields and associated values and errors.
Trait Implementations§
source§impl<'v, T: Debug> Debug for Contextual<'v, T>
impl<'v, T: Debug> Debug for Contextual<'v, T>
source§impl<'v, T: FromForm<'v>> FromForm<'v> for Contextual<'v, T>
impl<'v, T: FromForm<'v>> FromForm<'v> for Contextual<'v, T>
source§fn push_value((val_ctxt, ctxt): &mut Self::Context, field: ValueField<'v>)
fn push_value((val_ctxt, ctxt): &mut Self::Context, field: ValueField<'v>)
field
.source§fn push_data<'life0, 'life1, 'async_trait>(
(val_ctxt, ctxt): &'life0 mut Self::Context,
field: DataField<'v, 'life1>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'v: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn push_data<'life0, 'life1, 'async_trait>( (val_ctxt, ctxt): &'life0 mut Self::Context, field: DataField<'v, 'life1> ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where Self: 'async_trait, 'v: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,
field
.source§fn push_error((_, ctxt): &mut Self::Context, e: Error<'v>)
fn push_error((_, ctxt): &mut Self::Context, e: Error<'v>)
_error
. Read moreAuto Trait Implementations§
impl<'v, T> !RefUnwindSafe for Contextual<'v, T>
impl<'v, T> Send for Contextual<'v, T>where T: Send,
impl<'v, T> !Sync for Contextual<'v, T>
impl<'v, T> Unpin for Contextual<'v, T>where T: Unpin,
impl<'v, T> !UnwindSafe for Contextual<'v, T>
Blanket Implementations§
§impl<'a, T> AsTaggedExplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedExplicit<'a> for Twhere T: 'a,
§impl<'a, T> AsTaggedImplicit<'a> for Twhere
T: 'a,
impl<'a, T> AsTaggedImplicit<'a> for Twhere T: 'a,
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
§fn into_collection<A>(self) -> SmallVec<A>where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A>where A: Array<Item = T>,
self
into a collection.