Trait mango_orm::models::validation::AdditionalValidation
source · [−]pub trait AdditionalValidation {
fn add_validation<'a>(
&self
) -> Result<HashMap<&'a str, &'a str>, Box<dyn Error>> { ... }
}
Expand description
Methods for additional validation.
Hint: For custom use, add the Model/Form attribute is_use_add_valid = true
.
Hint (for models): Remember to use for validate of ignored fields.
Example:
impl AdditionalValidation for UserProfile {
fn add_validation<'a>(
&self,
) -> Result<std::collections::HashMap<&'a str, &'a str>, Box<dyn std::error::Error>> {
// Hint: error_map.insert("field_name", "Error message.")
let mut error_map: std::collections::HashMap<&'a str, &'a str> =
std::collections::HashMap::new();
// Get clean data
let hash = self.hash.clone().unwrap_or_default();
let password = self.password.clone().unwrap_or_default();
let confirm_password = self.confirm_password.clone().unwrap_or_default();
let username = self.username.clone().unwrap_or_default();
// Fields validation
if hash.is_empty() && password != confirm_password {
error_map.insert("confirm_password", "Password confirmation does not match.");
}
if !RegexBuilder::new(r"^[a-z\d_@+.]+$")
.case_insensitive(true)
.build()
.unwrap()
.is_match(username.as_str())
{
error_map.insert(
"username",
"Invalid characters present.<br>\
Valid characters: a-z A-Z 0-9 _ @ + .",
);
}
Ok(error_map)
}
}