pub struct FormErrors(/* private fields */);Available on crate feature
alloc only.Expand description
Every problem with one submitted form, for showing the form again with
a message beside each field: what
FromForm::from_body_all returns.
A parse error (a malformed body, a limit) stops the parse, so it is the only error; extraction errors are collected for every field, at most one per received value plus one per missing field. No value is ever kept.
§Examples
use form_net::{ExtractError, FromForm};
form_net::form! {
#[derive(Debug)]
pub struct Signup { pub email: String, pub age: u8, pub tos: bool }
}
let errors = Signup::from_body_all(b"application/x-www-form-urlencoded", b"age=300&role=admin").unwrap_err();
assert_eq!(errors.len(), 3);
assert_eq!(errors.field("age"), Some(ExtractError::Invalid("out of range")));
assert_eq!(errors.field("email"), Some(ExtractError::Missing));
assert_eq!(errors.unknown(), 1);
assert_eq!(errors.status_hint(), 422);Implementations§
Source§impl FormErrors
impl FormErrors
Sourcepub fn iter(&self) -> impl Iterator<Item = &FormError>
pub fn iter(&self) -> impl Iterator<Item = &FormError>
Every error, in the order found.
§Examples
use form_net::{ExtractError, FromForm};
let ct = b"application/x-www-form-urlencoded";
let errors = Signup::from_body_all(ct, b"age=300&role=admin").unwrap_err();
let codes: Vec<_> = errors.iter().map(|e| e.code()).collect();
assert_eq!(codes, ["extract.invalid", "extract.unknown", "extract.missing"]);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
How many errors.
§Examples
use form_net::{ExtractError, FromForm};
let ct = b"application/x-www-form-urlencoded";
let errors = Signup::from_body_all(ct, b"age=300&role=admin").unwrap_err();
assert_eq!(errors.len(), 3);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether there are none (never true for a returned value).
§Examples
use form_net::{ExtractError, FromForm};
let ct = b"application/x-www-form-urlencoded";
let errors = Signup::from_body_all(ct, b"age=300&role=admin").unwrap_err();
assert!(!errors.is_empty());Sourcepub fn first(&self) -> Option<&FormError>
pub fn first(&self) -> Option<&FormError>
The first error.
§Examples
use form_net::{ExtractError, FromForm};
let ct = b"application/x-www-form-urlencoded";
let errors = Signup::from_body_all(ct, b"age=300&role=admin").unwrap_err();
assert_eq!(errors.first().map(|e| e.code()), Some("extract.invalid"));Sourcepub fn field(&self, name: &str) -> Option<ExtractError>
pub fn field(&self, name: &str) -> Option<ExtractError>
The first extraction error for the field with this Rust name.
§Examples
use form_net::{ExtractError, FromForm};
let ct = b"application/x-www-form-urlencoded";
let errors = Signup::from_body_all(ct, b"age=300&role=admin").unwrap_err();
assert_eq!(errors.field("age"), Some(ExtractError::Invalid("out of range")));
assert_eq!(errors.field("email"), Some(ExtractError::Missing));Sourcepub fn unknown(&self) -> usize
pub fn unknown(&self) -> usize
How many received names the struct neither declares nor ignores.
§Examples
use form_net::{ExtractError, FromForm};
let ct = b"application/x-www-form-urlencoded";
let errors = Signup::from_body_all(ct, b"age=300&role=admin").unwrap_err();
// `role` is neither a field nor an ignored name.
assert_eq!(errors.unknown(), 1);Sourcepub fn status_hint(&self) -> u16
pub fn status_hint(&self) -> u16
The status hint of the first error (422 for extraction errors).
§Examples
use form_net::{ExtractError, FromForm};
let ct = b"application/x-www-form-urlencoded";
let errors = Signup::from_body_all(ct, b"age=300&role=admin").unwrap_err();
assert_eq!(errors.status_hint(), 422);Trait Implementations§
Source§impl Clone for FormErrors
impl Clone for FormErrors
Source§impl Debug for FormErrors
impl Debug for FormErrors
Source§impl Display for FormErrors
impl Display for FormErrors
impl Eq for FormErrors
Source§impl Error for FormErrors
Available on crate feature std only.
impl Error for FormErrors
Available on crate feature
std only.1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Source§impl From<FormError> for FormErrors
impl From<FormError> for FormErrors
Source§fn from(e: FormError) -> FormErrors
fn from(e: FormError) -> FormErrors
Converts to this type from the input type.
Source§impl PartialEq for FormErrors
impl PartialEq for FormErrors
impl StructuralPartialEq for FormErrors
Auto Trait Implementations§
impl Forget for FormErrors
impl Freeze for FormErrors
impl Move for FormErrors
impl RefUnwindSafe for FormErrors
impl Send for FormErrors
impl Sync for FormErrors
impl Unpin for FormErrors
impl UnsafeUnpin for FormErrors
impl UnwindSafe for FormErrors
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more