]> saetta.ns0.it Git - rust/zakform/commitdiff
Implemented validator date compare.
authorAndrea Zagli <azagli@libero.it>
Fri, 29 Sep 2023 16:44:16 +0000 (18:44 +0200)
committerAndrea Zagli <azagli@libero.it>
Fri, 29 Sep 2023 16:44:16 +0000 (18:44 +0200)
src/validators.rs

index 92a92986bd85bee0980b53f0bf59d71db9f9ecac..82c3db5991cfa9cf776843aec5bda5d68befb522 100644 (file)
@@ -110,9 +110,20 @@ impl ValidatorRegex {
        }
 }
 
+pub enum ValidatorDateCompareType {
+       Less,
+       LessEq,
+       Eq,
+       Great,
+       GreatEq,
+}
+
 #[derive(Default)]
 pub struct ValidatorDate {
        validator: Validator,
+       compare_type: Option<ValidatorDateCompareType>,
+       compare_value: Option<String>,
+       compare_value_date: Option<chrono::NaiveDate>,
 }
 
 impl TValidator for ValidatorDate {
@@ -129,11 +140,61 @@ impl TValidator for ValidatorDate {
        }
 
        fn validate(&self, value: &String) -> String {
-               let d = chrono::NaiveDate::parse_from_str(value, "%d/%m/%Y");
-               match d {
-                       Ok(_) => { return String::from(""); },
+               let d = match chrono::NaiveDate::parse_from_str(value, "%d/%m/%Y" ) {
+                       Ok(r) => { r },
                        Err(_) => { return self.get_message(); }
                };
+
+               let c: chrono::NaiveDate;
+               match &self.compare_value {
+                       Some(s) => {
+                               if s == "@now" {
+                                       let now = chrono::Local::now().naive_local();
+                                       c = chrono::NaiveDate::from(now);
+                               } else {
+                                       match self.compare_value_date {
+                                               Some(x) => { c = x; },
+                                               None => { return String::from(""); },
+                                       };
+                               }
+                       },
+                       None => { return String::from(""); },
+               };
+
+               match &self.compare_type {
+                       Some(t) => {
+                               match t {
+                                       ValidatorDateCompareType::Less => {
+                                               if d >= c {
+                                                       return self.get_message();
+                                               }
+                                       },
+                                       ValidatorDateCompareType::LessEq => {
+                                               if d > c {
+                                                       return self.get_message();
+                                               }
+                                       },
+                                       ValidatorDateCompareType::Eq => {
+                                               if d != c {
+                                                       return self.get_message();
+                                               }
+                                       },
+                                       ValidatorDateCompareType::Great => {
+                                               if d <= c {
+                                                       return self.get_message();
+                                               }
+                                       },
+                                       ValidatorDateCompareType::GreatEq => {
+                                               if d < c {
+                                                       return self.get_message();
+                                               }
+                                       },
+                               };
+                       },
+                       None => { },
+               };
+
+               return String::from("");
        }
 }
 
@@ -145,4 +206,37 @@ impl ValidatorDate {
 
                v
        }
+
+       pub fn get_compare_type(&self) -> &Option<ValidatorDateCompareType> {
+               &self.compare_type
+       }
+
+       pub fn set_compare_type(&mut self, compare_type: Option<ValidatorDateCompareType>) {
+               self.compare_type = compare_type;
+       }
+
+       pub fn get_compare_value(&self) -> &Option<String> {
+               &self.compare_value
+       }
+
+       pub fn set_compare_value(&mut self, compare_value: Option<String>) {
+               match compare_value {
+                       Some(c) => {
+                               if c == "@now" {
+                                       self.compare_value = Some(c);
+                                       self.compare_value_date = None; // convert on validate
+                               } else {
+                                       let d = chrono::NaiveDate::parse_from_str(&c, "%d/%m/%Y");
+                                       match d {
+                                               Ok(d) => {
+                                                       self.compare_value = Some(c);
+                                                       self.compare_value_date = Some(d);
+                                               },
+                                               Err(_) => { self.compare_value = None; }
+                                       };
+                               }
+                       },
+                       None => { self.compare_value = None; },
+               }
+       }
 }