Improve forms guide.

This commit is contained in:
Sergio Benitez 2021-03-11 01:12:20 -08:00
parent 8283bf0a2b
commit ef7b7a953e
1 changed files with 23 additions and 16 deletions

View File

@ -682,10 +682,13 @@ response is returned. The handler above is complete. It really is that simple!
## Forms ## Forms
Forms are one of the most common types of data handled in web applications, and Forms are one of the most common types of data handled in web applications, and
Rocket makes handling them easy. Say your application is processing a form Rocket makes handling them easy. Rocket supports both `multipart` and
submission for a new todo `Task`. The form contains two fields: `complete`, a `x-www-form-urlencoded` forms out of the box, enabled by the [`Form`] data guard
checkbox, and `type`, a text field. You can easily handle the form request in and derivable [`FromForm`] trait.
Rocket as follows:
Say your application is processing a form submission for a new todo `Task`. The
form contains two fields: `complete`, a checkbox, and `type`, a text field. You
can easily handle the form request in Rocket as follows:
```rust ```rust
# #[macro_use] extern crate rocket; # #[macro_use] extern crate rocket;
@ -841,31 +844,36 @@ to the function call. The rest of the fields are pass as written in the
expression. expression.
Any function in the [`form::validate`] module can be called, and other fields of Any function in the [`form::validate`] module can be called, and other fields of
the form can be passed in by using `self.$field` `$field` is the name of the the form can be passed in by using `self.$field` where `$field` is the name of
field in the structure. For example, the following form validates that the value the field in the structure. You can also apply more than one validation to a
of the field `confirm` is equal to the value of the field `value`: fiel by using multiple attributes. For example, the following form validates
that the value of the field `confirm` is equal to the value of the field `value`
and that it doesn't contain `no`:
```rust ```rust
# #[macro_use] extern crate rocket; # #[macro_use] extern crate rocket;
#[derive(FromForm)] #[derive(FromForm)]
struct Password { struct Password<'r> {
#[field(name = "password")] #[field(name = "password")]
value: String, value: &'r str,
#[field(validate = eq(&*self.value))] #[field(validate = eq(self.value))]
confirm: String, #[field(validate = omits("no"))]
confirm: &'r str,
} }
``` ```
[`form::validate`]: @api/rocket/form/validate/index.html [`form::validate`]: @api/rocket/form/validate/index.html
[`form::validate::range`]: @api/rocket/form/validate/fn.range.html [`form::validate::range`]: @api/rocket/form/validate/fn.range.html
[`form::Result`]: @api/rocket/form/type.Result.html
[`Errors<'_>`]: @api/rocket/form/error/struct.Errors.html [`Errors<'_>`]: @api/rocket/form/error/struct.Errors.html
In reality, the expression after `validate =` can be _any_ expression as long as In reality, the expression after `validate =` can be _any_ expression as long as
it evaluates to a value of type `Result<(), Errors<'_>>`, where an `Ok` value it evaluates to a value of type `Result<(), Errors<'_>>` (aliased by
means that validation was successful while an `Err` of [`Errors<'_>`] indicates [`form::Result`]), where an `Ok` value means that validation was successful while
the error(s) that occured. For instance, if you wanted to implement an ad-hoc an `Err` of [`Errors<'_>`] indicates the error(s) that occured. For instance, if
Luhn validator for credit-card-like numbers, you might write: you wanted to implement an ad-hoc Luhn validator for credit-card-like numbers,
you might write:
```rust ```rust
# #[macro_use] extern crate rocket; # #[macro_use] extern crate rocket;
@ -904,7 +912,6 @@ checkboxes, and `Option<T>`. Additionally, `FromForm` is implemented for
`Result<T, Errors<'_>>` where the error value is [`Errors<'_>`]. All of these `Result<T, Errors<'_>>` where the error value is [`Errors<'_>`]. All of these
types can be used just like any other form field: types can be used just like any other form field:
```rust ```rust
# use rocket::form::FromForm; # use rocket::form::FromForm;
use rocket::form::Errors; use rocket::form::Errors;