Struct rocket::form::Contextual[][src]

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

impl<'v, T: Debug> Debug for Contextual<'v, T>[src]

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

Formats the value using the given formatter. Read more

impl<'v, T: FromForm<'v>> FromForm<'v> for Contextual<'v, T>[src]

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

The form guard’s parsing context.

fn init(opts: Options) -> Self::Context[src]

Initializes and returns the parsing context for Self.

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

Processes the value field field.

fn push_data<'life0, 'life1, 'async_trait>(
    __arg0: &'life0 mut Self::Context,
    field: DataField<'v, 'life1>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>> where
    'v: 'async_trait,
    'life0: 'async_trait,
    'life1: 'async_trait,
    Self: 'async_trait, 
[src]

Processes the data field field.

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

Processes the external form or field error _error. Read more

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

Finalizes parsing. Returns the parsed value when successful or collection of Errors otherwise. Read more

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

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[src]

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

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

fn in_current_span(self) -> Instrumented<Self>[src]

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

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> IntoCollection<T> for T

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

Converts self into a collection.

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

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

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

pub fn vzip(self) -> V