db_type: FieldDbType,
tmpl: tera::Tera,
filters: Vec<super::filters::Filter>,
+ validators: Vec<Box<dyn super::validators::TValidator>>,
}
pub trait TField {
fn filter(&mut self);
+ fn add_validator(&mut self, validator: Box<dyn super::validators::TValidator>);
+
fn set_template(&mut self, template: &str);
fn render(&self) -> String;
}
}
+ fn add_validator(&mut self, validator: Box<dyn super::validators::TValidator>) {
+ self.field.validators.push(validator);
+ }
+
fn set_template(&mut self, template: &str) {
match self.field.tmpl.add_raw_template("field", template) {
Err(e) => { println!("{:?}", e) },
}
}
+ fn add_validator(&mut self, validator: Box<dyn super::validators::TValidator>) {
+ self.field.validators.push(validator);
+ }
+
fn set_template(&mut self, template: &str) {
match self.field.tmpl.add_raw_template("field", template) {
Err(e) => { println!("{:?}", e) },
}
}
+ fn add_validator(&mut self, validator: Box<dyn super::validators::TValidator>) {
+ self.field.validators.push(validator);
+ }
+
fn set_template(&mut self, template: &str) {
match self.field.tmpl.add_raw_template("field", template) {
Err(e) => { println!("{:?}", e) },
}
}
+ fn add_validator(&mut self, validator: Box<dyn super::validators::TValidator>) {
+ self.field.validators.push(validator);
+ }
+
fn set_template(&mut self, template: &str) {
match self.field.tmpl.add_raw_template("field", template) {
Err(e) => { println!("{:?}", e) },
}
}
+ fn add_validator(&mut self, validator: Box<dyn super::validators::TValidator>) {
+ self.field.validators.push(validator);
+ }
+
fn set_template(&mut self, template: &str) {
match self.field.tmpl.add_raw_template("field", template) {
Err(e) => { println!("{:?}", e) },
}
}
+ fn add_validator(&mut self, validator: Box<dyn super::validators::TValidator>) {
+ self.field.validators.push(validator);
+ }
+
fn set_template(&mut self, template: &str) {
match self.field.tmpl.add_raw_template("field", template) {
Err(e) => { println!("{:?}", e) },
--- /dev/null
+use std::default::Default;
+
+#[derive(Default)]
+struct Validator {
+ name: String,
+}
+
+pub trait TValidator {
+ fn get_name(&self) -> String;
+
+ fn validate(&self, value: &String) -> bool;
+}
+
+#[derive(Default)]
+pub struct ValidatorNotEmpty {
+ validator: Validator,
+}
+
+impl TValidator for ValidatorNotEmpty {
+ fn get_name(&self) -> String {
+ String::from(&self.validator.name)
+ }
+
+ fn validate(&self, value: &String) -> bool {
+ if value != "" {
+ true
+ } else {
+ false
+ }
+ }
+}
+
+impl ValidatorNotEmpty {
+ pub fn new(name: &String) -> Self {
+ let mut v: ValidatorNotEmpty = Default::default();
+ v.validator.name = String::from(name);
+
+ v
+ }
+}
+
+#[derive(Default)]
+pub struct ValidatorRegex {
+ validator: Validator,
+ regex: String,
+}
+
+impl TValidator for ValidatorRegex {
+ fn get_name(&self) -> String {
+ String::from(&self.validator.name)
+ }
+
+ fn validate(&self, value: &String) -> bool {
+ true
+ }
+}
+
+impl ValidatorRegex {
+ pub fn new(name: &String, regex: &str) -> Self {
+ let mut v: ValidatorRegex = Default::default();
+ v.validator.name = String::from(name);
+ v.regex = String::from(regex);
+
+ v
+ }
+
+ pub fn get_regex(&self) -> String {
+ String::from(&self.regex)
+ }
+
+ pub fn set_regex(&mut self, regex: &str) {
+ self.regex = String::from(regex);
+ }
+}