]> saetta.ns0.it Git - rust/zakform/commitdiff
Started number validator.
authorAndrea Zagli <azagli@libero.it>
Sat, 30 Sep 2023 07:48:23 +0000 (09:48 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 30 Sep 2023 07:48:23 +0000 (09:48 +0200)
po/it.po
src/validators.rs

index 1e197a983f715133898a1e14c18d7310a38d9d3c..12d97acf5e33ce584519825e0dd7eae0927d1e7f 100644 (file)
--- a/po/it.po
+++ b/po/it.po
@@ -26,3 +26,6 @@ msgstr "Valore non valido"
 
 msgid "Invalid date"
 msgstr "Data non valida"
+
+msgid "Invalid number"
+msgstr "Numero non valido"
index 82c3db5991cfa9cf776843aec5bda5d68befb522..f5ee2dab9f5a22574454a68bffff159f0256618d 100644 (file)
@@ -240,3 +240,133 @@ impl ValidatorDate {
                }
        }
 }
+
+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;
+       }
+}