pub struct Errors { /* private fields */ }Expand description
The messages a failed validation produced, grouped by field.
wants_json and back are captured when the errors are built from a
request, because IntoResponse::into_response no longer has the request
to negotiate with or to read a Referer from.
Implementations§
Source§impl Errors
impl Errors
pub fn new() -> Self
pub fn add(&mut self, field: impl Into<String>, message: impl Into<String>)
pub fn has(&self, field: &str) -> bool
Sourcepub fn first(&self, field: &str) -> Option<&str>
pub fn first(&self, field: &str) -> Option<&str>
The first message for a field — what a form renders next to the input.
Sourcepub fn all(&self) -> &BTreeMap<String, Vec<String>>
pub fn all(&self) -> &BTreeMap<String, Vec<String>>
Every message for every field, keyed by field name.
pub fn is_empty(&self) -> bool
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
The number of messages, not the number of fields — one field can fail several rules.
pub fn fields(&self) -> impl Iterator<Item = &str>
Sourcepub fn wants_json(&self) -> bool
pub fn wants_json(&self) -> bool
Whether the response should be JSON. Set from Request::wants_json.
Sourcepub fn redirecting_to(self, back: Option<String>) -> Self
pub fn redirecting_to(self, back: Option<String>) -> Self
Send a browser back here instead of rendering the messages as text.
pub fn with_json(self, wants_json: bool) -> Self
Sourcepub fn with(self, field: impl Into<String>, message: impl Into<String>) -> Self
pub fn with(self, field: impl Into<String>, message: impl Into<String>) -> Self
Add a message by hand, chaining — useful for a rule an application enforces itself, so its failure comes back in the same envelope as the built-in ones.
Sourcepub fn add_interpolated(
&mut self,
messages: &Messages,
field: &str,
template: &str,
)
pub fn add_interpolated( &mut self, messages: &Messages, field: &str, template: &str, )
Render a hand-written message through the message bag’s label rules, so
it interpolates :attribute the way a built-in one does.
Sourcepub fn summary(&self) -> String
pub fn summary(&self) -> String
The single-line summary Laravel puts in the message key: the first
failure, and a count of the rest so a client that only shows one line
still tells the user there is more to fix.
Sourcepub fn to_field_json(&self) -> Json
pub fn to_field_json(&self) -> Json
The Laravel-shaped 422 body.
Just the field map, as a template reads it: {"email": ["…"]}.
Errors::to_json wraps this in Laravel’s {"message", "errors"}
envelope, which is right for an API response and one level too deep for
a view.
pub fn to_json(&self) -> Json
Trait Implementations§
Source§impl Error for Errors
impl Error for Errors
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()
Source§impl IntoResponse for Errors
A 422 for an API client; for a browser, a redirect back to the form.
impl IntoResponse for Errors
A 422 for an API client; for a browser, a redirect back to the form.
The two halves answer different questions. A JSON client asked for a result and gets one, with the status that says why. A browser asked for a page, and the useful answer is the form it just submitted, with the messages attached and the boxes still filled in.
It is a redirect rather than the page itself because the answer to a failed
POST has to leave the browser somewhere reloadable. Rendering in place
leaves it on a URL that re-submits the form on refresh, which is the
double-submission problem in miniature — and it is why the messages travel
through the session rather than in this response.
With no session to leave them in, this falls back to plain text. That is a degraded answer rather than a broken one, and it is what an application with no session middleware gets.