pub struct InvalidInput { /* private fields */ }Expand description
Field errors for invalid input, with messages keyed by field path, such as user.email.
As a response, InvalidInput is a 422 Unprocessable Content. A JSON request receives this
body:
{ "message": "The email field is required.", "errors": { "email": ["The email field is required."] } }A browser request with a session gets a redirect back with the errors and old input instead,
no matter which code created the InvalidInput. Request::parse
returns an InvalidInput that names each field that doesn’t fit the type, and
Request::validate adds the errors from the declared rules.
Serializing an InvalidInput writes only the messages, keyed by field.
use laravel::validation::InvalidInput;
let errors = InvalidInput::new().with("email", "This email address is already taken.");
assert_eq!(errors.errors()["email"], ["This email address is already taken."]);A page with several forms uses for_form to show each form’s errors next to
that form. Errors without a form name belong to the default form. When a handler returns them
after validating a type with #[validate(form = "..")], such as with [invalid!], they belong
to that type’s form instead, like the rule errors from the same request.
Implementations§
Source§impl InvalidInput
impl InvalidInput
Sourcepub fn new() -> InvalidInput
pub fn new() -> InvalidInput
Returns an empty set of errors without a form name.
Sourcepub fn with(
self,
field: impl Into<String>,
message: impl Into<String>,
) -> InvalidInput
pub fn with( self, field: impl Into<String>, message: impl Into<String>, ) -> InvalidInput
Adds a message for a field and returns the errors, for building them in one expression. A field may have several messages.
Sourcepub fn add(
&mut self,
field: impl Into<String>,
message: impl Into<String>,
) -> &mut InvalidInput
pub fn add( &mut self, field: impl Into<String>, message: impl Into<String>, ) -> &mut InvalidInput
Adds a message for a field, after any messages it already has.
Sourcepub const fn errors(&self) -> &IndexMap<String, Vec<String>>
pub const fn errors(&self) -> &IndexMap<String, Vec<String>>
Returns the messages by field, in the order they were added.
Sourcepub fn for_form(self, form: impl Into<String>) -> InvalidInput
pub fn for_form(self, form: impl Into<String>) -> InvalidInput
Names the form the errors belong to, for a page with several forms.
Views read a named form’s errors with error("email", form="login"). An explicit name
takes precedence over the request’s _form field and the validated type’s name. JSON
responses don’t include the form’s name.
use laravel::validation::InvalidInput;
let errors = InvalidInput::new()
.with("email", "These credentials do not match our records.")
.for_form("login");
assert_eq!(errors.form(), "login");Trait Implementations§
Source§impl Clone for InvalidInput
impl Clone for InvalidInput
Source§fn clone(&self) -> InvalidInput
fn clone(&self) -> InvalidInput
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for InvalidInput
impl Debug for InvalidInput
Source§impl Default for InvalidInput
impl Default for InvalidInput
Source§fn default() -> InvalidInput
fn default() -> InvalidInput
Source§impl<'de> Deserialize<'de> for InvalidInput
impl<'de> Deserialize<'de> for InvalidInput
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<InvalidInput, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<InvalidInput, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Display for InvalidInput
Writes the first message and the number of remaining messages, such as
“The name field is required. (and 1 more error)”, or “The given data was invalid.” when there
are no messages.
impl Display for InvalidInput
Writes the first message and the number of remaining messages, such as “The name field is required. (and 1 more error)”, or “The given data was invalid.” when there are no messages.
impl Eq for InvalidInput
Source§impl Error for InvalidInput
impl Error for InvalidInput
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 PartialEq for InvalidInput
impl PartialEq for InvalidInput
Source§impl Serialize for InvalidInput
impl Serialize for InvalidInput
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl StructuralPartialEq for InvalidInput
Auto Trait Implementations§
impl Forget for InvalidInput
impl Freeze for InvalidInputwhere
*const u8 is !null: Freeze,
PhantomData<usize>: Freeze,
PhantomData<Bucket<String, Vec<String>>>: Freeze,
PhantomData<u8>: Freeze,
impl Move for InvalidInput
impl RefUnwindSafe for InvalidInput
impl Send for InvalidInput
impl Sync for InvalidInput
impl Unpin for InvalidInput
impl UnsafeUnpin for InvalidInputwhere
*const u8 is !null: UnsafeUnpin,
PhantomData<usize>: UnsafeUnpin,
PhantomData<Bucket<String, Vec<String>>>: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
impl UnwindSafe for InvalidInput
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<K> ForeignKey<K> for K
impl<K> ForeignKey<K> for K
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
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 more