FormValidator and HTMLBuilder now uses my string and vector aswell.

This commit is contained in:
Relintai 2021-11-01 18:00:34 +01:00
parent cc5962bbdd
commit dd966c9248
6 changed files with 110 additions and 119 deletions

View File

@ -1,11 +1,10 @@
#include "form_validator.h"
#include "core/http/request.h"
#include <sstream>
//FormFieldEntry
bool FormFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
bool FormFieldEntry::validate(Request *request, const FormField *field, const String &data, Vector<String> *errors) {
return true;
}
@ -17,7 +16,7 @@ FormFieldEntry::~FormFieldEntry() {
//FormExistsFieldEntry
bool FormExistsFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
bool FormExistsFieldEntry::validate(Request *request, const FormField *field, const String &data, Vector<String> *errors) {
if (data == "") {
if (errors) {
errors->push_back(field->human_name + not_exists_error);
@ -37,7 +36,7 @@ FormExistsFieldEntry::~FormExistsFieldEntry() {
//FormIntFieldEntry
bool FormIntFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
bool FormIntFieldEntry::validate(Request *request, const FormField *field, const String &data, Vector<String> *errors) {
//https://stackoverflow.com/questions/2844817/how-do-i-check-if-a-c-string-is-an-int
if (data.empty()) {
@ -75,7 +74,7 @@ FormIntFieldEntry::~FormIntFieldEntry() {
//FormFloatFieldEntry
bool FormFloatFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
bool FormFloatFieldEntry::validate(Request *request, const FormField *field, const String &data, Vector<String> *errors) {
if (data.empty()) {
return true;
}
@ -102,7 +101,7 @@ FormFloatFieldEntry::~FormFloatFieldEntry() {
//FormAlphaFieldEntry
bool FormAlphaFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
bool FormAlphaFieldEntry::validate(Request *request, const FormField *field, const String &data, Vector<String> *errors) {
for (int i = 0; i < data.size(); ++i) {
if (!isalpha(data[i])) {
if (errors) {
@ -124,7 +123,7 @@ FormAlphaFieldEntry::~FormAlphaFieldEntry() {
//FormAlphaNumericFieldEntry
bool FormAlphaNumericFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
bool FormAlphaNumericFieldEntry::validate(Request *request, const FormField *field, const String &data, Vector<String> *errors) {
for (int i = 0; i < data.size(); ++i) {
if (!isalnum(data[i])) {
if (errors) {
@ -146,7 +145,7 @@ FormAlphaNumericFieldEntry::~FormAlphaNumericFieldEntry() {
//FormNeedsLowercaseCharacterFieldEntry
bool FormNeedsLowercaseCharacterFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
bool FormNeedsLowercaseCharacterFieldEntry::validate(Request *request, const FormField *field, const String &data, Vector<String> *errors) {
for (int i = 0; i < data.size(); ++i) {
if (islower(data[i])) {
@ -169,7 +168,7 @@ FormNeedsLowercaseCharacterFieldEntry::~FormNeedsLowercaseCharacterFieldEntry()
//FormNeedsUppercaseCharacterFieldEntry
bool FormNeedsUppercaseCharacterFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
bool FormNeedsUppercaseCharacterFieldEntry::validate(Request *request, const FormField *field, const String &data, Vector<String> *errors) {
for (int i = 0; i < data.size(); ++i) {
if (isupper(data[i])) {
return true;
@ -191,7 +190,7 @@ FormNeedsUppercaseCharacterFieldEntry::~FormNeedsUppercaseCharacterFieldEntry()
//FormNeedsOtherCharacterFieldEntry
bool FormNeedsOtherCharacterFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
bool FormNeedsOtherCharacterFieldEntry::validate(Request *request, const FormField *field, const String &data, Vector<String> *errors) {
for (int i = 0; i < data.size(); ++i) {
if (!isalnum(data[i])) {
return true;
@ -213,14 +212,10 @@ FormNeedsOtherCharacterFieldEntry::~FormNeedsOtherCharacterFieldEntry() {
//FormMinimumLengthFieldEntry
bool FormMinimumLengthFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
bool FormMinimumLengthFieldEntry::validate(Request *request, const FormField *field, const String &data, Vector<String> *errors) {
if (data.size() < min_length) {
if (errors) {
std::stringstream ss;
ss << field->human_name << does_not_have_min_length_errorf << min_length << does_not_have_min_length_errors;
errors->push_back(ss.str());
errors->push_back(field->human_name + does_not_have_min_length_errorf + min_length + does_not_have_min_length_errors);
}
return false;
@ -240,14 +235,10 @@ FormMinimumLengthFieldEntry::~FormMinimumLengthFieldEntry() {
//FormMaximumLengthFieldEntry
bool FormMaximumLengthFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
bool FormMaximumLengthFieldEntry::validate(Request *request, const FormField *field, const String &data, Vector<String> *errors) {
if (data.size() > max_length) {
if (errors) {
std::stringstream ss;
ss << field->human_name << does_not_have_max_length_errorf << max_length << does_not_have_max_length_errors;
errors->push_back(ss.str());
errors->push_back(field->human_name + does_not_have_max_length_errorf + max_length + does_not_have_max_length_errors);
}
return false;
@ -267,7 +258,7 @@ FormMaximumLengthFieldEntry::~FormMaximumLengthFieldEntry() {
//FormEmailFieldEntry
bool FormEmailFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
bool FormEmailFieldEntry::validate(Request *request, const FormField *field, const String &data, Vector<String> *errors) {
if (data.size() == 0) {
if (errors) {
errors->push_back(field->human_name + email_format_error);
@ -360,7 +351,7 @@ FormEmailFieldEntry::~FormEmailFieldEntry() {
//FormNeedToMatchOtherFieldEntry
bool FormNeedToMatchOtherFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
bool FormNeedToMatchOtherFieldEntry::validate(Request *request, const FormField *field, const String &data, Vector<String> *errors) {
if (data != request->get_parameter(other_field)) {
if (errors) {
errors->push_back(field->human_name + does_not_match_error + field->name + ".");
@ -440,7 +431,7 @@ FormField *FormField::need_to_be_email() {
return this;
}
FormField *FormField::need_to_match(const std::string &other) {
FormField *FormField::need_to_match(const String &other) {
FormNeedToMatchOtherFieldEntry *f = new FormNeedToMatchOtherFieldEntry();
f->other_field = other;
add_entry(f);
@ -454,7 +445,7 @@ FormField *FormField::ignore_if_not_exists() {
return this;
}
FormField *FormField::ignore_if_other_field_not_exists(const std::string &other) {
FormField *FormField::ignore_if_other_field_not_exists(const String &other) {
_ignore_if_other_field_not_exists = true;
_ignore_if_other_field_not_exist_field = other;
@ -465,15 +456,15 @@ void FormField::add_entry(FormFieldEntry *field) {
fields.push_back(field);
}
bool FormField::validate(Request *request, std::vector<std::string> *errors) {
std::string param = request->get_parameter(name);
bool FormField::validate(Request *request, Vector<String> *errors) {
String param = request->get_parameter(name);
if (_ignore_if_not_exists && param == "") {
return true;
}
if (_ignore_if_other_field_not_exists) {
std::string op = request->get_parameter(_ignore_if_other_field_not_exist_field);
String op = request->get_parameter(_ignore_if_other_field_not_exist_field);
if (op == "") {
return true;
@ -505,7 +496,7 @@ FormField::~FormField() {
//FormValidator
bool FormValidator::validate(Request *request, std::vector<std::string> *errors) {
bool FormValidator::validate(Request *request, Vector<String> *errors) {
bool valid = true;
for (int i = 0; i < fields.size(); ++i) {
@ -522,7 +513,7 @@ void FormValidator::add_field(FormField *field) {
fields.push_back(field);
}
FormField *FormValidator::new_field(const std::string &name, const std::string &human_name) {
FormField *FormValidator::new_field(const String &name, const String &human_name) {
FormField *f = new FormField();
f->name = name;
f->human_name = human_name;

View File

@ -1,17 +1,17 @@
#ifndef FORM_H
#define FORM_H
#include "core/string.h"
#include "core/containers/vector.h"
#include <ctype.h>
#include <map>
#include <string>
#include <vector>
class Request;
class FormField;
class FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
FormFieldEntry();
virtual ~FormFieldEntry();
@ -19,143 +19,143 @@ public:
class FormExistsFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
FormExistsFieldEntry();
~FormExistsFieldEntry();
std::string not_exists_error;
String not_exists_error;
};
class FormIntFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
FormIntFieldEntry();
~FormIntFieldEntry();
std::string not_int_error;
String not_int_error;
};
class FormFloatFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
FormFloatFieldEntry();
~FormFloatFieldEntry();
std::string not_float_error;
String not_float_error;
};
class FormAlphaFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
FormAlphaFieldEntry();
~FormAlphaFieldEntry();
std::string not_alpha_error;
String not_alpha_error;
};
class FormAlphaNumericFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
FormAlphaNumericFieldEntry();
~FormAlphaNumericFieldEntry();
std::string not_alpha_numeric_error;
String not_alpha_numeric_error;
};
class FormNeedsLowercaseCharacterFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
FormNeedsLowercaseCharacterFieldEntry();
~FormNeedsLowercaseCharacterFieldEntry();
std::string does_not_have_lowercase_error;
String does_not_have_lowercase_error;
};
class FormNeedsUppercaseCharacterFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
FormNeedsUppercaseCharacterFieldEntry();
~FormNeedsUppercaseCharacterFieldEntry();
std::string does_not_have_uppercase_error;
String does_not_have_uppercase_error;
};
class FormNeedsOtherCharacterFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
FormNeedsOtherCharacterFieldEntry();
~FormNeedsOtherCharacterFieldEntry();
std::string does_not_have_other_error;
String does_not_have_other_error;
};
class FormMinimumLengthFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
FormMinimumLengthFieldEntry();
~FormMinimumLengthFieldEntry();
int min_length;
std::string does_not_have_min_length_errorf;
std::string does_not_have_min_length_errors;
String does_not_have_min_length_errorf;
String does_not_have_min_length_errors;
};
class FormMaximumLengthFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
FormMaximumLengthFieldEntry();
~FormMaximumLengthFieldEntry();
int max_length;
std::string does_not_have_max_length_errorf;
std::string does_not_have_max_length_errors;
String does_not_have_max_length_errorf;
String does_not_have_max_length_errors;
};
class FormEmailFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
FormEmailFieldEntry();
~FormEmailFieldEntry();
std::string email_format_error;
String email_format_error;
};
class FormNeedToMatchOtherFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
FormNeedToMatchOtherFieldEntry();
~FormNeedToMatchOtherFieldEntry();
std::string other_field;
String other_field;
std::string does_not_match_error;
String does_not_match_error;
};
//FormField
class FormField {
public:
std::string name;
std::string human_name;
String name;
String human_name;
bool _ignore_if_not_exists;
bool _ignore_if_other_field_not_exists;
std::string _ignore_if_other_field_not_exist_field;
String _ignore_if_other_field_not_exist_field;
FormField *need_to_exist();
FormField *need_to_be_int();
@ -168,33 +168,33 @@ public:
FormField *need_minimum_length(const int min_length);
FormField *need_maximum_length(const int max_length);
FormField *need_to_be_email();
FormField *need_to_match(const std::string &other);
FormField *need_to_match(const String &other);
FormField *ignore_if_not_exists();
FormField *ignore_if_other_field_not_exists(const std::string &other);
FormField *ignore_if_other_field_not_exists(const String &other);
void add_entry(FormFieldEntry *field);
bool validate(Request *request, std::vector<std::string> *errors);
bool validate(Request *request, Vector<String> *errors);
FormField();
virtual ~FormField();
std::vector<FormFieldEntry *> fields;
Vector<FormFieldEntry *> fields;
};
//FormValidator
class FormValidator {
public:
bool validate(Request *request, std::vector<std::string> *errors = nullptr);
bool validate(Request *request, Vector<String> *errors = nullptr);
void add_field(FormField *field);
FormField *new_field(const std::string &name, const std::string &human_name);
FormField *new_field(const String &name, const String &human_name);
FormValidator();
virtual ~FormValidator();
std::vector<FormField *> fields;
Vector<FormField *> fields;
};
#endif

View File

@ -1,72 +1,72 @@
#include "html_builder.h"
HTMLTag *HTMLTag::str(const std::string &str) {
HTMLTag *HTMLTag::str(const String &str) {
result += " " + str;
return this;
}
HTMLTag *HTMLTag::style(const std::string &val) {
HTMLTag *HTMLTag::style(const String &val) {
attrib("style", val);
return this;
}
HTMLTag *HTMLTag::href(const std::string &val) {
HTMLTag *HTMLTag::href(const String &val) {
attrib("href", val);
return this;
}
HTMLTag *HTMLTag::cls(const std::string &val) {
HTMLTag *HTMLTag::cls(const String &val) {
attrib("class", val);
return this;
}
HTMLTag *HTMLTag::id(const std::string &val) {
HTMLTag *HTMLTag::id(const String &val) {
attrib("id", val);
return this;
}
HTMLTag *HTMLTag::name(const std::string &val) {
HTMLTag *HTMLTag::name(const String &val) {
attrib("name", val);
return this;
}
HTMLTag *HTMLTag::content(const std::string &val) {
HTMLTag *HTMLTag::content(const String &val) {
attrib("content", val);
return this;
}
HTMLTag *HTMLTag::value(const std::string &val) {
HTMLTag *HTMLTag::value(const String &val) {
attrib("value", val);
return this;
}
HTMLTag *HTMLTag::method(const std::string &val) {
HTMLTag *HTMLTag::method(const String &val) {
attrib("method", val);
return this;
}
HTMLTag *HTMLTag::type(const std::string &val) {
HTMLTag *HTMLTag::type(const String &val) {
attrib("type", val);
return this;
}
HTMLTag *HTMLTag::placeholder(const std::string &val) {
HTMLTag *HTMLTag::placeholder(const String &val) {
attrib("placeholder", val);
return this;
}
HTMLTag *HTMLTag::rel(const std::string &val) {
HTMLTag *HTMLTag::rel(const String &val) {
attrib("rel", val);
return this;
@ -78,7 +78,7 @@ HTMLTag *HTMLTag::rel_stylesheet() {
return this;
}
HTMLTag *HTMLTag::charset(const std::string &val) {
HTMLTag *HTMLTag::charset(const String &val) {
attrib("charset", val);
return this;
@ -90,13 +90,13 @@ HTMLTag *HTMLTag::charset_utf_8() {
return this;
}
HTMLTag *HTMLTag::attrib(const std::string &attr, const std::string &val) {
HTMLTag *HTMLTag::attrib(const String &attr, const String &val) {
result += " " + attr + "=\"" + val + "\"";
return this;
}
HTMLTag *HTMLTag::start(const std::string &p_tag, const bool p_simple) {
HTMLTag *HTMLTag::start(const String &p_tag, const bool p_simple) {
simple = p_simple;
result = "<" + p_tag;
@ -127,7 +127,7 @@ HTMLTag::HTMLTag() {
simple = true;
}
void HTMLBuilder::comment(const std::string &val) {
void HTMLBuilder::comment(const String &val) {
write_tag();
result += "<!--" + val + "-->";
@ -139,7 +139,7 @@ HTMLTag *HTMLBuilder::doctype() {
return tag.start("!DOCTYPE");
}
void HTMLBuilder::doctype(const std::string &val) {
void HTMLBuilder::doctype(const String &val) {
write_tag();
result += "<!DOCTYPE " + val + ">";
@ -1470,14 +1470,14 @@ void HTMLBuilder::cwbr() {
result += "</wbr>";
}
void HTMLBuilder::w(const std::string &val) {
void HTMLBuilder::w(const String &val) {
write_tag();
result += val;
}
//TODO!
void HTMLBuilder::we(const std::string &val) {
void HTMLBuilder::we(const String &val) {
printf("HTMLBuilder::write_excaped NYI!");
write_tag();

View File

@ -1,9 +1,7 @@
#ifndef HTML_BUILDER_H
#define HTML_BUILDER_H
#include <map>
#include <string>
#include <vector>
#include "core/string.h"
class Request;
@ -12,27 +10,27 @@ class Request;
class HTMLTag {
public:
bool simple;
std::string result;
String result;
HTMLTag *str(const std::string &str);
HTMLTag *style(const std::string &val);
HTMLTag *href(const std::string &val);
HTMLTag *cls(const std::string &val);
HTMLTag *id(const std::string &val);
HTMLTag *name(const std::string &val);
HTMLTag *content(const std::string &val);
HTMLTag *value(const std::string &val);
HTMLTag *method(const std::string &val);
HTMLTag *type(const std::string &val);
HTMLTag *placeholder(const std::string &val);
HTMLTag *rel(const std::string &val);
HTMLTag *str(const String &str);
HTMLTag *style(const String &val);
HTMLTag *href(const String &val);
HTMLTag *cls(const String &val);
HTMLTag *id(const String &val);
HTMLTag *name(const String &val);
HTMLTag *content(const String &val);
HTMLTag *value(const String &val);
HTMLTag *method(const String &val);
HTMLTag *type(const String &val);
HTMLTag *placeholder(const String &val);
HTMLTag *rel(const String &val);
HTMLTag *rel_stylesheet();
HTMLTag *charset(const std::string &val);
HTMLTag *charset(const String &val);
HTMLTag *charset_utf_8();
HTMLTag *attrib(const std::string &attr, const std::string &val);
HTMLTag *attrib(const String &attr, const String &val);
HTMLTag *start(const std::string &p_new_tag, const bool p_simple = false);
HTMLTag *start(const String &p_new_tag, const bool p_simple = false);
HTMLTag *reset();
HTMLTag *close();
@ -43,11 +41,11 @@ public:
class HTMLBuilder {
public:
std::string result;
String result;
void comment(const std::string &val);
void comment(const String &val);
HTMLTag *doctype();
void doctype(const std::string &val);
void doctype(const String &val);
HTMLTag *a();
HTMLTag *abbr();
@ -306,9 +304,9 @@ public:
void cwbr();
//write
void w(const std::string &val);
void w(const String &val);
//write_escaped
void we(const std::string &val);
void we(const String &val);
void write_tag();

View File

@ -45,7 +45,7 @@ void UserController::handle_login_request_default(Request *request) {
//this is probbaly not needed
//it's ok for now as I need to test the validators more
std::vector<std::string> errors;
Vector<String> errors;
_login_validator->validate(request, &errors);
for (int i = 0; i < errors.size(); ++i) {
data.error_str += errors[i] + "<br>";
@ -134,7 +134,7 @@ void UserController::handle_register_request_default(Request *request) {
if (request->get_method() == HTTP_METHOD_POST) {
std::vector<std::string> errors;
Vector<String> errors;
_registration_validator->validate(request, &errors);
@ -310,7 +310,7 @@ void UserController::handle_settings_request(Ref<User> &user, Request *request)
bool changed = false;
std::vector<std::string> errors;
Vector<String> errors;
bool valid = _profile_validator->validate(request, &errors);

View File

@ -1,9 +1,11 @@
#ifndef USER_CONTROLLER_H
#define USER_CONTROLLER_H
#include "core/string.h"
#include "core/containers/vector.h"
#include "core/object.h"
#include <string>
#include "user.h"
class Request;