[[package]]
name = "regex"
-version = "1.9.5"
+version = "1.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47"
+checksum = "ebee201405406dbf528b8b672104ae6d6d63e6d118cb10e4d51abbc7b58044ff"
dependencies = [
"aho-corasick",
"memchr",
[[package]]
name = "regex-automata"
-version = "0.3.8"
+version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795"
+checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9"
dependencies = [
"aho-corasick",
"memchr",
"config",
"gettext",
"lazy_static",
+ "regex",
"serde",
"serde_derive",
"serde_json",
use std::default::Default;
+use regex::Regex;
+
use chrono::NaiveDate;
#[derive(Default)]
pub struct ValidatorRegex {
validator: Validator,
regex: String,
+ re: Option<Regex>,
}
impl TValidator for ValidatorRegex {
}
fn validate(&self, value: &String) -> String {
- String::from("")
+ match &self.re {
+ Some(re) => {
+ if re.is_match(value) {
+ String::from("")
+ } else {
+ self.get_message()
+ }
+ },
+ None => { String::from("") },
+ }
}
}
pub fn new(name: &str, regex: &str) -> Self {
let mut v: ValidatorRegex = Default::default();
v.validator.name = String::from(name);
- v.validator.message = String::from("Invalid value");
- v.regex = String::from(regex);
+ v.validator.message = String::from(super::tr.gettext("Invalid value"));
+ v.set_regex(regex);
v
}
pub fn set_regex(&mut self, regex: &str) {
self.regex = String::from(regex);
+ self.re = Some(Regex::new(regex).unwrap());
}
}