}
}
}
+
+pub enum ValidatorNumberCompareType {
+ Less,
+ LessEq,
+ Eq,
+ Great,
+ GreatEq,
+}
+
+#[derive(Default)]
+pub struct ValidatorNumber {
+ validator: Validator,
+ compare_type: Option<ValidatorNumberCompareType>,
+ compare_value: f64,
+ plus_minus: bool,
+ thousands_separator: Option<char>,
+ decimals_separator: Option<char>,
+}
+
+impl TValidator for ValidatorNumber {
+ fn get_name(&self) -> String {
+ String::from(&self.validator.name)
+ }
+
+ fn get_message(&self) -> String {
+ String::from(&self.validator.message)
+ }
+
+ fn set_message(&mut self, message: &str) {
+ self.validator.message = String::from(message);
+ }
+
+ fn validate(&self, value: &String) -> String {
+ let tesc: String;
+ let desc: String;
+
+ let mut sregex = String::from("^");
+
+ if self.plus_minus {
+ sregex.push_str("[+-]?");
+ }
+
+ match self.thousands_separator {
+ Some(t) => {
+ if t == '.' {
+ tesc = String::from("\\.");
+ } else {
+ tesc = format!("{}", t);
+ }
+ },
+ None => { tesc = String::new(); },
+ }
+
+ match self.decimals_separator {
+ Some(t) => {
+ if t == '.' {
+ desc = String::from("\\.");
+ } else {
+ desc = format!("{}", t);
+ }
+ },
+ None => { desc = String::new(); },
+ }
+
+ sregex.push_str(format!("\\d{{1,3}}({}\\d{{3}})*({}\\d+)?$|^[+-]?\\d+({}\\d+)?$",
+ tesc,
+ desc,
+ desc).as_str());
+ println!("REGEX {} - {}", sregex, value);
+ let re = Regex::new(&sregex).unwrap();
+
+ if !re.is_match(value) {
+ return self.get_message();
+ }
+
+ return String::from("");
+ }
+}
+
+impl ValidatorNumber {
+ pub fn new(name: &str) -> Self {
+ let mut v: ValidatorNumber = Default::default();
+ v.validator.name = String::from(name);
+ v.validator.message = String::from(super::tr.gettext("Invalid number"));
+ v.thousands_separator = Some('.');
+ v.decimals_separator = Some(',');
+
+ v
+ }
+
+ pub fn get_compare_type(&self) -> &Option<ValidatorNumberCompareType> {
+ &self.compare_type
+ }
+
+ pub fn set_compare_type(&mut self, compare_type: Option<ValidatorNumberCompareType>) {
+ self.compare_type = compare_type;
+ }
+
+ pub fn get_compare_value(&self) -> f64 {
+ self.compare_value
+ }
+
+ pub fn set_compare_value(&mut self, compare_value: f64) {
+ self.compare_value = compare_value;
+ }
+
+ pub fn get_plus_mius(&self) -> bool {
+ self.plus_minus
+ }
+
+ pub fn set_plus_minus(&mut self, plus_minus: bool) {
+ self.plus_minus = plus_minus;
+ }
+
+ pub fn get_thousands_separator(&self) -> &Option<char> {
+ &self.thousands_separator
+ }
+
+ pub fn set_thousands_separator(&mut self, thousands_separator: Option<char>) {
+ self.thousands_separator = thousands_separator;
+ }
+
+ pub fn get_decimals_separator(&self) -> &Option<char> {
+ &self.decimals_separator
+ }
+
+ pub fn set_decimals_separator(&mut self, decimals_separator: Option<char>) {
+ self.decimals_separator = decimals_separator;
+ }
+}