}
fn validate_std(f: &Field) -> String {
- let mut s = String::from("<ul>\n");
+ let mut first = true;
+
+ let mut s = String::from("");
for v in &f.validators {
let msg = v.validate(&f.value);
if msg != "" {
+ if first {
+ s.push_str("<ul>\n");
+ first = false;
+ }
+
s.push_str(format!("<li>{}</li>\n", &msg).as_str());
}
}
- s.push_str("</ul>");
+ if s != "" {
+ s.push_str("</ul>");
+ }
s
}
+use crate::fields;
+
pub struct Form {
fields: Vec<Box<dyn super::fields::TField>>,
}
Err(())
}
+
+ pub fn validate(&mut self) -> bool {
+ let mut r = true;
+
+ for f in &mut self.fields {
+ let msg = f.validate();
+ if msg != "" {
+ r = false;
+ f.set_help(&msg);
+ }
+ }
+
+ r
+ }
}
}
fn validate(&self, value: &String) -> String {
- if value != "" {
+ if value == "" {
self.get_message()
} else {
String::from("")
}
impl ValidatorNotEmpty {
- pub fn new(name: &String) -> Self {
+ pub fn new(name: &str) -> Self {
let mut v: ValidatorNotEmpty = Default::default();
v.validator.name = String::from(name);
v.validator.message = String::from("Mandatory field");
}
impl ValidatorRegex {
- pub fn new(name: &String, regex: &str) -> Self {
+ 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");