pub struct ValidationError { /* private fields */ }Expand description
A collection of validation errors.
Carries an optional old_input payload for the flash round-trip; set via
with_old_input() before calling redirect_back() or redirect_to().
Implementations§
Source§impl ValidationError
impl ValidationError
Sourcepub fn add(&mut self, field: &str, message: impl Into<String>)
pub fn add(&mut self, field: &str, message: impl Into<String>)
Add an error message for a field.
Sourcepub fn into_messages(self) -> HashMap<String, Vec<String>>
pub fn into_messages(self) -> HashMap<String, Vec<String>>
Consume the error and return the inner HashMap of field -> messages. Useful for passing errors to templates.
Sourcepub fn with_old_input(self, data: &Value) -> Self
pub fn with_old_input(self, data: &Value) -> Self
Attach submitted form values as “old input” for the next GET request.
Chain before redirect_back() or redirect_to(). The caller typically
passes the same serde_json::Value used to construct the Validator.
§Example
if let Err(e) = validator.validate() {
return e.with_old_input(&form_data).redirect_back(referer);
}Sourcepub fn redirect_back(self, referer: Option<&str>) -> Response
pub fn redirect_back(self, referer: Option<&str>) -> Response
Flash errors + old input into the session, then redirect to referer.
Falls back to "/" when referer is None or when the Referer header
contains a non-same-origin URL (T-92-05 mitigation).
§Example
if let Err(e) = validator.validate() {
let referer = req.header("Referer");
return e.with_old_input(&form_data).redirect_back(referer);
}Sourcepub fn redirect_to(self, url: impl Into<String>) -> Response
pub fn redirect_to(self, url: impl Into<String>) -> Response
Flash errors + old input into the session, then redirect to an explicit URL.
Use this instead of redirect_back() when the calling controller knows
the exact destination (e.g. a tabbed settings form where the Referer may
lack the ?tab=... parameter).
§Example
if let Err(e) = validator.validate() {
return e.with_old_input(&form_data).redirect_to("/settings?tab=generale");
}Trait Implementations§
Source§impl Clone for ValidationError
impl Clone for ValidationError
Source§fn clone(&self) -> ValidationError
fn clone(&self) -> ValidationError
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ValidationError
impl Debug for ValidationError
Source§impl Default for ValidationError
impl Default for ValidationError
Source§fn default() -> ValidationError
fn default() -> ValidationError
Source§impl Display for ValidationError
impl Display for ValidationError
Source§impl Error for ValidationError
impl Error for ValidationError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Auto Trait Implementations§
impl Freeze for ValidationError
impl RefUnwindSafe for ValidationError
impl Send for ValidationError
impl Sync for ValidationError
impl Unpin for ValidationError
impl UnsafeUnpin for ValidationError
impl UnwindSafe for ValidationError
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.