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>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'v, T: FromForm<'v>> FromForm<'v> for Contextual<'v, T>

§

type Context = (<T as FromForm<'v>>::Context, Context<'v>)

The form guard’s parsing context.
source§

fn init(opts: Options) -> Self::Context

Initializes and returns the parsing context for Self.
source§

fn push_value((val_ctxt, ctxt): &mut Self::Context, field: ValueField<'v>)

Processes the value field 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,

Processes the data field field.
source§

fn push_error((_, ctxt): &mut Self::Context, e: Error<'v>)

Processes the external form or field error _error. Read more
source§

fn finalize((val_ctxt, context): Self::Context) -> Result<'v, Self>

Finalizes parsing. Returns the parsed value when successful or collection of Errors otherwise.
source§

fn default(opts: Options) -> Option<Self>

Returns a default value, if any, to use when a value is desired and parsing fails. Read more

Auto 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§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T> AsTaggedExplicit<'a> for Twhere T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self>

§

impl<'a, T> AsTaggedImplicit<'a> for Twhere T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self>

source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoCollection<T> for T

source§

fn into_collection<A>(self) -> SmallVec<A>where A: Array<Item = T>,

Converts self into a collection.
source§

fn mapped<U, F, A>(self, f: F) -> SmallVec<A>where F: FnMut(T) -> U, A: Array<Item = U>,

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more