pub struct ValidationError<Key> {
pub key: Key,
pub type_id: &'static str,
/* private fields */
}
Expand description
An error associated with a form field.
Fields§
§key: Key
The key for the field that this validation error is associated with.
type_id: &'static str
An identifier for the type of error this is.
Implementations§
Source§impl<Key> ValidationError<Key>
impl<Key> ValidationError<Key>
Sourcepub fn new(key: Key, type_id: &'static str) -> Self
pub fn new(key: Key, type_id: &'static str) -> Self
Create a new ValidationError
with a generic message, and
specify the type_id which allows
the error type to be identified programatically.
Sourcepub fn message<S: Into<String>>(self, message: S) -> Self
pub fn message<S: Into<String>>(self, message: S) -> Self
Factory method to set the message for this error.
Sourcepub fn with_message<F: Fn(&Key) -> String + 'static>(
self,
message_fn: F,
) -> Self
pub fn with_message<F: Fn(&Key) -> String + 'static>( self, message_fn: F, ) -> Self
Factory method to set the message for this error from a
function that returns a String
.
§Example
use form_validation::ValidationError;
let value = -10;
let error = ValidationError::new("field1", "NOT_LESS_THAN_0")
.with_message(move |key| {
format!(
"The value of {} ({}) cannot be less than 0",
key, value)
});
assert_eq!("The value of field1 (-10) cannot be less than 0", error.to_string());
assert_eq!("NOT_LESS_THAN_0", error.type_id);
Trait Implementations§
Source§impl<Key> Clone for ValidationError<Key>where
Key: Clone,
impl<Key> Clone for ValidationError<Key>where
Key: Clone,
Source§impl<Key> Debug for ValidationError<Key>where
Key: Debug,
impl<Key> Debug for ValidationError<Key>where
Key: Debug,
Source§impl<Key> Display for ValidationError<Key>
impl<Key> Display for ValidationError<Key>
Source§impl<Key> Error for ValidationError<Key>where
Key: Debug,
impl<Key> Error for ValidationError<Key>where
Key: Debug,
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<Key> From<ValidationError<Key>> for ValidationErrors<Key>
impl<Key> From<ValidationError<Key>> for ValidationErrors<Key>
Source§fn from(err: ValidationError<Key>) -> Self
fn from(err: ValidationError<Key>) -> Self
Converts to this type from the input type.
Source§impl<Key> PartialEq for ValidationError<Key>where
Key: PartialEq,
impl<Key> PartialEq for ValidationError<Key>where
Key: PartialEq,
Auto Trait Implementations§
impl<Key> Freeze for ValidationError<Key>where
Key: Freeze,
impl<Key> !RefUnwindSafe for ValidationError<Key>
impl<Key> !Send for ValidationError<Key>
impl<Key> !Sync for ValidationError<Key>
impl<Key> Unpin for ValidationError<Key>where
Key: Unpin,
impl<Key> !UnwindSafe for ValidationError<Key>
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