pub macro msg { ($e:expr) => { ... }, ($($arg:tt)*) => { ... }, }
Expand description
A helper macro for custom validation error messages.
The macro works similar to std::format!
. It generates a form
Validation
error message. While useful in other contexts, it is
designed to be chained to validation results in derived FromForm
#[field]
attributes via .or_else()
and .and_then()
.
§Example
use rocket::form::FromForm;
#[derive(FromForm)]
struct Person<'r> {
#[field(validate = len(3..).or_else(msg!("that's a short name...")))]
name: &'r str,
#[field(validate = contains('f').and_then(msg!("please, no `f`!")))]
non_f_name: &'r str,
}
Note: this macro never needs to be imported when used with a
FromForm
derive; all items in form::validate
are automatically in
scope in FromForm
derive attributes.
See the top-level docs for more examples.
§Syntax
The macro has the following “signatures”:
§Variant 1
fn msg<'a, T, P, E: Expr>(expr: E) -> impl Fn(P) -> form::Result<'a, T>
Takes any expression and returns a function that takes any argument type
and evaluates to a form::Result
with an Ok
of
any type. The Result
is guaranteed to be an Err
of kind
Validation
with expr
as the message.
§Variant 2
fn msg<'a, T, P, A: Args>(fmt: &str, args: A) -> impl Fn(P) -> form::Result<'a, T>
Invokes the first variant as msg!(format!(fmt, args))
.