Trait under::FromForm

source ·
pub trait FromForm: Sized {
    fn from_form<'f, I, K, V>(form: I) -> Result<Self, FromFormError>
    where
        I: Iterator<Item = (K, V)>,
        K: AsRef<str> + 'f,
        V: AsRef<str> + 'f
; }
Available on crate feature from_form only.
Expand description

A trait for types that can be created from a form.

This is for parsing from a HTML form, not a URL query string. Specifically, this parses from the body of a application/x-www-form-urlencoded request. Because form-urlencoded is not a strict data format, e.g. it has no nesting structure, this trait is not implemented for FromFormValue and instead provides a from_form method. It is not only allowed that the keys are repeated, but expected for a few things - especially arrays.

It is expected that you use #[derive(FromForm)] for any type that must implement this trait. This will generate a from_form method that correctly parses the types from the form. If you need to implement this trait by hand, you will need to use FromFormValue to parse each value (and FromFormMultiple for arrays).

Examples


#[derive(FromForm)]
struct LoginForm {
   username: String,
  password: String,
}

let form = LoginForm::from_form([
   ("username", "Sergio"),
  ("password", "hunter2"),
].into_iter()).unwrap();

Required Methods§

Takes in an iterator of key-values, and returns a Result<Self, FromFormError>. The iterator is guaranteed to be in the order that the keys were encountered in the form. This is important for arrays, which are expected to be repeated keys.

Implementations on Foreign Types§

Available on crate feature from_form only.

Implementors§