pub struct FormErrors { /* private fields */ }Expand description
Form-validation error envelope. Wraps the ORM’s WriteError so
every surface (REST 400 bodies, admin form spans, HTML form
renders) sees the same structured shape. The template helper
as_template_ctx produces the flat single-string-per-field view
that most form templates ask for.
Not Clone because WriteError carries a sqlx::Error variant
that’s also not Clone. If you need a cheap copyable bundle of
rendered messages, use Self::as_template_ctx which returns
an owned serde_json::Map.
Implementations§
Source§impl FormErrors
impl FormErrors
Sourcepub fn new(err: WriteError) -> Self
pub fn new(err: WriteError) -> Self
Wrap any WriteError. Use From for free conversion in
? chains. The raw values default to empty — call
Self::with_raw when you have the submitted pairs in
scope (typically only inside an axum extractor).
Sourcepub fn with_raw(err: WriteError, raw: HashMap<String, String>) -> Self
pub fn with_raw(err: WriteError, raw: HashMap<String, String>) -> Self
Construct a FormErrors carrying both the validation
failure AND the raw form pairs the user submitted. The raw
pairs let the handler re-render the form pre-filled with
what the user typed instead of falling back to
T::default() (which loses every keystroke).
Sourcepub fn raw_values(&self) -> &HashMap<String, String>
pub fn raw_values(&self) -> &HashMap<String, String>
Borrow the raw form pairs the user submitted, if the
extractor captured them. Empty when the FormErrors was
constructed via Self::new or any From impl that
doesn’t see the request body.
Sourcepub fn raw_as_json(&self) -> Value
pub fn raw_as_json(&self) -> Value
JSON-shaped view of the raw values, ready to drop straight
into a template context as the form key so existing
{{ form.<field> }} references repopulate the user’s
input. The map is String → String so every value
serialises to a JSON string — templates that need typed
access should call Self::raw_values and convert per
field.
Sourcepub fn as_write_error(&self) -> &WriteError
pub fn as_write_error(&self) -> &WriteError
Borrow the underlying WriteError — keeps every accessor
available (field_errors(), non_field_errors(),
error_code()).
Sourcepub fn into_write_error(self) -> WriteError
pub fn into_write_error(self) -> WriteError
Move out the underlying WriteError (e.g. to feed a
REST-style error-body builder).
Sourcepub fn field_errors(&self) -> BTreeMap<String, Vec<String>>
pub fn field_errors(&self) -> BTreeMap<String, Vec<String>>
Per-field error map — see WriteError::field_errors.
Sourcepub fn non_field_errors(&self) -> Vec<String>
pub fn non_field_errors(&self) -> Vec<String>
Cross-field / non-field error list — see
WriteError::non_field_errors.
Sourcepub fn as_template_ctx(&self) -> Map<String, Value>
pub fn as_template_ctx(&self) -> Map<String, Value>
Template-friendly flat view: each field maps to its FIRST
error message (string), plus the FIRST non-field error under
the form key. Renders directly under the errors context
key — templates write {{ errors.name }} or
{% if errors.form %}.
For templates that need to render EVERY error per field
(rare), call [field_errors] / [non_field_errors]
directly and pass the maps as-is.
Sourcepub fn render(&self, template: &str) -> Response
pub fn render(&self, template: &str) -> Response
Render template with this failed submission bound into scope
and return the complete HTTP response. The one-liner for a form
handler’s Err arm:
let msg = match form.into_result() {
Ok(v) => v,
Err(errs) => return Ok(errs.render("contact.html")),
};What the template sees:
form— the raw pairs the user submitted, so{{ form.<field> }}repopulates every keystroke.errors— the flat per-field view fromSelf::as_template_ctx, plus a default form-level summary undererrors.form(“Please fix the highlighted fields and try again.”) when no non-field error supplied one — every form page wants the banner, so the framework defaults it.- Anything ambient (
csrf_token/csrf_input/user) via the normal render merge.
Status is 422 Unprocessable Entity. A template failure
returns a plain 500 carrying the render error. Extra context
keys (page flags, chrome): Self::render_with.
Sourcepub fn render_with(&self, template: &str, extra: Map<String, Value>) -> Response
pub fn render_with(&self, template: &str, extra: Map<String, Value>) -> Response
Self::render plus caller-supplied top-level context keys.
extra wins over the form / errors bindings on key
collision — the caller is more specific than the default.
Trait Implementations§
Source§impl Debug for FormErrors
impl Debug for FormErrors
Source§impl Display for FormErrors
impl Display for FormErrors
Source§impl Error for FormErrors
impl Error for FormErrors
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 From<ValidationErrors> for FormErrors
impl From<ValidationErrors> for FormErrors
Source§fn from(e: ValidationErrors) -> Self
fn from(e: ValidationErrors) -> Self
Source§impl From<WriteError> for FormErrors
impl From<WriteError> for FormErrors
Source§fn from(e: WriteError) -> Self
fn from(e: WriteError) -> Self
Auto Trait Implementations§
impl !RefUnwindSafe for FormErrors
impl !UnwindSafe for FormErrors
impl Freeze for FormErrors
impl Send for FormErrors
impl Sync for FormErrors
impl Unpin for FormErrors
impl UnsafeUnpin 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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
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> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);