mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-05-02 13:47:56 +02:00
FormValidator and HTMLBuilder now uses my string and vector aswell.
This commit is contained in:
parent
cc5962bbdd
commit
dd966c9248
@ -1,11 +1,10 @@
|
|||||||
#include "form_validator.h"
|
#include "form_validator.h"
|
||||||
|
|
||||||
#include "core/http/request.h"
|
#include "core/http/request.h"
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
//FormFieldEntry
|
//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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,7 +16,7 @@ FormFieldEntry::~FormFieldEntry() {
|
|||||||
|
|
||||||
//FormExistsFieldEntry
|
//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 (data == "") {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(field->human_name + not_exists_error);
|
errors->push_back(field->human_name + not_exists_error);
|
||||||
@ -37,7 +36,7 @@ FormExistsFieldEntry::~FormExistsFieldEntry() {
|
|||||||
|
|
||||||
//FormIntFieldEntry
|
//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
|
//https://stackoverflow.com/questions/2844817/how-do-i-check-if-a-c-string-is-an-int
|
||||||
|
|
||||||
if (data.empty()) {
|
if (data.empty()) {
|
||||||
@ -75,7 +74,7 @@ FormIntFieldEntry::~FormIntFieldEntry() {
|
|||||||
|
|
||||||
//FormFloatFieldEntry
|
//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()) {
|
if (data.empty()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -102,7 +101,7 @@ FormFloatFieldEntry::~FormFloatFieldEntry() {
|
|||||||
|
|
||||||
//FormAlphaFieldEntry
|
//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) {
|
for (int i = 0; i < data.size(); ++i) {
|
||||||
if (!isalpha(data[i])) {
|
if (!isalpha(data[i])) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
@ -124,7 +123,7 @@ FormAlphaFieldEntry::~FormAlphaFieldEntry() {
|
|||||||
|
|
||||||
//FormAlphaNumericFieldEntry
|
//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) {
|
for (int i = 0; i < data.size(); ++i) {
|
||||||
if (!isalnum(data[i])) {
|
if (!isalnum(data[i])) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
@ -146,7 +145,7 @@ FormAlphaNumericFieldEntry::~FormAlphaNumericFieldEntry() {
|
|||||||
|
|
||||||
//FormNeedsLowercaseCharacterFieldEntry
|
//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) {
|
for (int i = 0; i < data.size(); ++i) {
|
||||||
if (islower(data[i])) {
|
if (islower(data[i])) {
|
||||||
|
|
||||||
@ -169,7 +168,7 @@ FormNeedsLowercaseCharacterFieldEntry::~FormNeedsLowercaseCharacterFieldEntry()
|
|||||||
|
|
||||||
//FormNeedsUppercaseCharacterFieldEntry
|
//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) {
|
for (int i = 0; i < data.size(); ++i) {
|
||||||
if (isupper(data[i])) {
|
if (isupper(data[i])) {
|
||||||
return true;
|
return true;
|
||||||
@ -191,7 +190,7 @@ FormNeedsUppercaseCharacterFieldEntry::~FormNeedsUppercaseCharacterFieldEntry()
|
|||||||
|
|
||||||
//FormNeedsOtherCharacterFieldEntry
|
//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) {
|
for (int i = 0; i < data.size(); ++i) {
|
||||||
if (!isalnum(data[i])) {
|
if (!isalnum(data[i])) {
|
||||||
return true;
|
return true;
|
||||||
@ -213,14 +212,10 @@ FormNeedsOtherCharacterFieldEntry::~FormNeedsOtherCharacterFieldEntry() {
|
|||||||
|
|
||||||
//FormMinimumLengthFieldEntry
|
//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 (data.size() < min_length) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
std::stringstream ss;
|
errors->push_back(field->human_name + does_not_have_min_length_errorf + min_length + does_not_have_min_length_errors);
|
||||||
|
|
||||||
ss << field->human_name << does_not_have_min_length_errorf << min_length << does_not_have_min_length_errors;
|
|
||||||
|
|
||||||
errors->push_back(ss.str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -240,14 +235,10 @@ FormMinimumLengthFieldEntry::~FormMinimumLengthFieldEntry() {
|
|||||||
|
|
||||||
//FormMaximumLengthFieldEntry
|
//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 (data.size() > max_length) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
std::stringstream ss;
|
errors->push_back(field->human_name + does_not_have_max_length_errorf + max_length + does_not_have_max_length_errors);
|
||||||
|
|
||||||
ss << field->human_name << does_not_have_max_length_errorf << max_length << does_not_have_max_length_errors;
|
|
||||||
|
|
||||||
errors->push_back(ss.str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -267,7 +258,7 @@ FormMaximumLengthFieldEntry::~FormMaximumLengthFieldEntry() {
|
|||||||
|
|
||||||
//FormEmailFieldEntry
|
//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 (data.size() == 0) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(field->human_name + email_format_error);
|
errors->push_back(field->human_name + email_format_error);
|
||||||
@ -360,7 +351,7 @@ FormEmailFieldEntry::~FormEmailFieldEntry() {
|
|||||||
|
|
||||||
//FormNeedToMatchOtherFieldEntry
|
//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 (data != request->get_parameter(other_field)) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(field->human_name + does_not_match_error + field->name + ".");
|
errors->push_back(field->human_name + does_not_match_error + field->name + ".");
|
||||||
@ -440,7 +431,7 @@ FormField *FormField::need_to_be_email() {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
FormField *FormField::need_to_match(const std::string &other) {
|
FormField *FormField::need_to_match(const String &other) {
|
||||||
FormNeedToMatchOtherFieldEntry *f = new FormNeedToMatchOtherFieldEntry();
|
FormNeedToMatchOtherFieldEntry *f = new FormNeedToMatchOtherFieldEntry();
|
||||||
f->other_field = other;
|
f->other_field = other;
|
||||||
add_entry(f);
|
add_entry(f);
|
||||||
@ -454,7 +445,7 @@ FormField *FormField::ignore_if_not_exists() {
|
|||||||
return this;
|
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_exists = true;
|
||||||
_ignore_if_other_field_not_exist_field = other;
|
_ignore_if_other_field_not_exist_field = other;
|
||||||
|
|
||||||
@ -465,15 +456,15 @@ void FormField::add_entry(FormFieldEntry *field) {
|
|||||||
fields.push_back(field);
|
fields.push_back(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FormField::validate(Request *request, std::vector<std::string> *errors) {
|
bool FormField::validate(Request *request, Vector<String> *errors) {
|
||||||
std::string param = request->get_parameter(name);
|
String param = request->get_parameter(name);
|
||||||
|
|
||||||
if (_ignore_if_not_exists && param == "") {
|
if (_ignore_if_not_exists && param == "") {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_ignore_if_other_field_not_exists) {
|
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 == "") {
|
if (op == "") {
|
||||||
return true;
|
return true;
|
||||||
@ -505,7 +496,7 @@ FormField::~FormField() {
|
|||||||
|
|
||||||
//FormValidator
|
//FormValidator
|
||||||
|
|
||||||
bool FormValidator::validate(Request *request, std::vector<std::string> *errors) {
|
bool FormValidator::validate(Request *request, Vector<String> *errors) {
|
||||||
bool valid = true;
|
bool valid = true;
|
||||||
|
|
||||||
for (int i = 0; i < fields.size(); ++i) {
|
for (int i = 0; i < fields.size(); ++i) {
|
||||||
@ -522,7 +513,7 @@ void FormValidator::add_field(FormField *field) {
|
|||||||
fields.push_back(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();
|
FormField *f = new FormField();
|
||||||
f->name = name;
|
f->name = name;
|
||||||
f->human_name = human_name;
|
f->human_name = human_name;
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
#ifndef FORM_H
|
#ifndef FORM_H
|
||||||
#define FORM_H
|
#define FORM_H
|
||||||
|
|
||||||
|
#include "core/string.h"
|
||||||
|
#include "core/containers/vector.h"
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <map>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class Request;
|
class Request;
|
||||||
class FormField;
|
class FormField;
|
||||||
|
|
||||||
class FormFieldEntry {
|
class FormFieldEntry {
|
||||||
public:
|
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();
|
FormFieldEntry();
|
||||||
virtual ~FormFieldEntry();
|
virtual ~FormFieldEntry();
|
||||||
@ -19,143 +19,143 @@ public:
|
|||||||
|
|
||||||
class FormExistsFieldEntry : public FormFieldEntry {
|
class FormExistsFieldEntry : public FormFieldEntry {
|
||||||
public:
|
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();
|
||||||
~FormExistsFieldEntry();
|
~FormExistsFieldEntry();
|
||||||
|
|
||||||
std::string not_exists_error;
|
String not_exists_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormIntFieldEntry : public FormFieldEntry {
|
class FormIntFieldEntry : public FormFieldEntry {
|
||||||
public:
|
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();
|
||||||
~FormIntFieldEntry();
|
~FormIntFieldEntry();
|
||||||
|
|
||||||
std::string not_int_error;
|
String not_int_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormFloatFieldEntry : public FormFieldEntry {
|
class FormFloatFieldEntry : public FormFieldEntry {
|
||||||
public:
|
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();
|
||||||
~FormFloatFieldEntry();
|
~FormFloatFieldEntry();
|
||||||
|
|
||||||
std::string not_float_error;
|
String not_float_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormAlphaFieldEntry : public FormFieldEntry {
|
class FormAlphaFieldEntry : public FormFieldEntry {
|
||||||
public:
|
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();
|
||||||
~FormAlphaFieldEntry();
|
~FormAlphaFieldEntry();
|
||||||
|
|
||||||
std::string not_alpha_error;
|
String not_alpha_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormAlphaNumericFieldEntry : public FormFieldEntry {
|
class FormAlphaNumericFieldEntry : public FormFieldEntry {
|
||||||
public:
|
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();
|
||||||
~FormAlphaNumericFieldEntry();
|
~FormAlphaNumericFieldEntry();
|
||||||
|
|
||||||
std::string not_alpha_numeric_error;
|
String not_alpha_numeric_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormNeedsLowercaseCharacterFieldEntry : public FormFieldEntry {
|
class FormNeedsLowercaseCharacterFieldEntry : public FormFieldEntry {
|
||||||
public:
|
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();
|
||||||
~FormNeedsLowercaseCharacterFieldEntry();
|
~FormNeedsLowercaseCharacterFieldEntry();
|
||||||
|
|
||||||
std::string does_not_have_lowercase_error;
|
String does_not_have_lowercase_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormNeedsUppercaseCharacterFieldEntry : public FormFieldEntry {
|
class FormNeedsUppercaseCharacterFieldEntry : public FormFieldEntry {
|
||||||
public:
|
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();
|
||||||
~FormNeedsUppercaseCharacterFieldEntry();
|
~FormNeedsUppercaseCharacterFieldEntry();
|
||||||
|
|
||||||
std::string does_not_have_uppercase_error;
|
String does_not_have_uppercase_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormNeedsOtherCharacterFieldEntry : public FormFieldEntry {
|
class FormNeedsOtherCharacterFieldEntry : public FormFieldEntry {
|
||||||
public:
|
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();
|
||||||
~FormNeedsOtherCharacterFieldEntry();
|
~FormNeedsOtherCharacterFieldEntry();
|
||||||
|
|
||||||
std::string does_not_have_other_error;
|
String does_not_have_other_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormMinimumLengthFieldEntry : public FormFieldEntry {
|
class FormMinimumLengthFieldEntry : public FormFieldEntry {
|
||||||
public:
|
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();
|
||||||
~FormMinimumLengthFieldEntry();
|
~FormMinimumLengthFieldEntry();
|
||||||
|
|
||||||
int min_length;
|
int min_length;
|
||||||
|
|
||||||
std::string does_not_have_min_length_errorf;
|
String does_not_have_min_length_errorf;
|
||||||
std::string does_not_have_min_length_errors;
|
String does_not_have_min_length_errors;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormMaximumLengthFieldEntry : public FormFieldEntry {
|
class FormMaximumLengthFieldEntry : public FormFieldEntry {
|
||||||
public:
|
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();
|
||||||
~FormMaximumLengthFieldEntry();
|
~FormMaximumLengthFieldEntry();
|
||||||
|
|
||||||
int max_length;
|
int max_length;
|
||||||
|
|
||||||
std::string does_not_have_max_length_errorf;
|
String does_not_have_max_length_errorf;
|
||||||
std::string does_not_have_max_length_errors;
|
String does_not_have_max_length_errors;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormEmailFieldEntry : public FormFieldEntry {
|
class FormEmailFieldEntry : public FormFieldEntry {
|
||||||
public:
|
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();
|
||||||
~FormEmailFieldEntry();
|
~FormEmailFieldEntry();
|
||||||
|
|
||||||
std::string email_format_error;
|
String email_format_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FormNeedToMatchOtherFieldEntry : public FormFieldEntry {
|
class FormNeedToMatchOtherFieldEntry : public FormFieldEntry {
|
||||||
public:
|
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();
|
||||||
~FormNeedToMatchOtherFieldEntry();
|
~FormNeedToMatchOtherFieldEntry();
|
||||||
|
|
||||||
std::string other_field;
|
String other_field;
|
||||||
|
|
||||||
std::string does_not_match_error;
|
String does_not_match_error;
|
||||||
};
|
};
|
||||||
|
|
||||||
//FormField
|
//FormField
|
||||||
|
|
||||||
class FormField {
|
class FormField {
|
||||||
public:
|
public:
|
||||||
std::string name;
|
String name;
|
||||||
std::string human_name;
|
String human_name;
|
||||||
|
|
||||||
bool _ignore_if_not_exists;
|
bool _ignore_if_not_exists;
|
||||||
|
|
||||||
bool _ignore_if_other_field_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_exist();
|
||||||
FormField *need_to_be_int();
|
FormField *need_to_be_int();
|
||||||
@ -168,33 +168,33 @@ public:
|
|||||||
FormField *need_minimum_length(const int min_length);
|
FormField *need_minimum_length(const int min_length);
|
||||||
FormField *need_maximum_length(const int max_length);
|
FormField *need_maximum_length(const int max_length);
|
||||||
FormField *need_to_be_email();
|
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_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);
|
void add_entry(FormFieldEntry *field);
|
||||||
|
|
||||||
bool validate(Request *request, std::vector<std::string> *errors);
|
bool validate(Request *request, Vector<String> *errors);
|
||||||
|
|
||||||
FormField();
|
FormField();
|
||||||
virtual ~FormField();
|
virtual ~FormField();
|
||||||
|
|
||||||
std::vector<FormFieldEntry *> fields;
|
Vector<FormFieldEntry *> fields;
|
||||||
};
|
};
|
||||||
|
|
||||||
//FormValidator
|
//FormValidator
|
||||||
|
|
||||||
class FormValidator {
|
class FormValidator {
|
||||||
public:
|
public:
|
||||||
bool validate(Request *request, std::vector<std::string> *errors = nullptr);
|
bool validate(Request *request, Vector<String> *errors = nullptr);
|
||||||
|
|
||||||
void add_field(FormField *field);
|
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();
|
FormValidator();
|
||||||
virtual ~FormValidator();
|
virtual ~FormValidator();
|
||||||
|
|
||||||
std::vector<FormField *> fields;
|
Vector<FormField *> fields;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -1,72 +1,72 @@
|
|||||||
#include "html_builder.h"
|
#include "html_builder.h"
|
||||||
|
|
||||||
HTMLTag *HTMLTag::str(const std::string &str) {
|
HTMLTag *HTMLTag::str(const String &str) {
|
||||||
result += " " + str;
|
result += " " + str;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLTag *HTMLTag::style(const std::string &val) {
|
HTMLTag *HTMLTag::style(const String &val) {
|
||||||
attrib("style", val);
|
attrib("style", val);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLTag *HTMLTag::href(const std::string &val) {
|
HTMLTag *HTMLTag::href(const String &val) {
|
||||||
attrib("href", val);
|
attrib("href", val);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLTag *HTMLTag::cls(const std::string &val) {
|
HTMLTag *HTMLTag::cls(const String &val) {
|
||||||
attrib("class", val);
|
attrib("class", val);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLTag *HTMLTag::id(const std::string &val) {
|
HTMLTag *HTMLTag::id(const String &val) {
|
||||||
attrib("id", val);
|
attrib("id", val);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLTag *HTMLTag::name(const std::string &val) {
|
HTMLTag *HTMLTag::name(const String &val) {
|
||||||
attrib("name", val);
|
attrib("name", val);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLTag *HTMLTag::content(const std::string &val) {
|
HTMLTag *HTMLTag::content(const String &val) {
|
||||||
attrib("content", val);
|
attrib("content", val);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLTag *HTMLTag::value(const std::string &val) {
|
HTMLTag *HTMLTag::value(const String &val) {
|
||||||
attrib("value", val);
|
attrib("value", val);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLTag *HTMLTag::method(const std::string &val) {
|
HTMLTag *HTMLTag::method(const String &val) {
|
||||||
attrib("method", val);
|
attrib("method", val);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLTag *HTMLTag::type(const std::string &val) {
|
HTMLTag *HTMLTag::type(const String &val) {
|
||||||
attrib("type", val);
|
attrib("type", val);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLTag *HTMLTag::placeholder(const std::string &val) {
|
HTMLTag *HTMLTag::placeholder(const String &val) {
|
||||||
attrib("placeholder", val);
|
attrib("placeholder", val);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLTag *HTMLTag::rel(const std::string &val) {
|
HTMLTag *HTMLTag::rel(const String &val) {
|
||||||
attrib("rel", val);
|
attrib("rel", val);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
@ -78,7 +78,7 @@ HTMLTag *HTMLTag::rel_stylesheet() {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
HTMLTag *HTMLTag::charset(const std::string &val) {
|
HTMLTag *HTMLTag::charset(const String &val) {
|
||||||
attrib("charset", val);
|
attrib("charset", val);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
@ -90,13 +90,13 @@ HTMLTag *HTMLTag::charset_utf_8() {
|
|||||||
return this;
|
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 + "\"";
|
result += " " + attr + "=\"" + val + "\"";
|
||||||
|
|
||||||
return this;
|
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;
|
simple = p_simple;
|
||||||
|
|
||||||
result = "<" + p_tag;
|
result = "<" + p_tag;
|
||||||
@ -127,7 +127,7 @@ HTMLTag::HTMLTag() {
|
|||||||
simple = true;
|
simple = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HTMLBuilder::comment(const std::string &val) {
|
void HTMLBuilder::comment(const String &val) {
|
||||||
write_tag();
|
write_tag();
|
||||||
|
|
||||||
result += "<!--" + val + "-->";
|
result += "<!--" + val + "-->";
|
||||||
@ -139,7 +139,7 @@ HTMLTag *HTMLBuilder::doctype() {
|
|||||||
return tag.start("!DOCTYPE");
|
return tag.start("!DOCTYPE");
|
||||||
}
|
}
|
||||||
|
|
||||||
void HTMLBuilder::doctype(const std::string &val) {
|
void HTMLBuilder::doctype(const String &val) {
|
||||||
write_tag();
|
write_tag();
|
||||||
|
|
||||||
result += "<!DOCTYPE " + val + ">";
|
result += "<!DOCTYPE " + val + ">";
|
||||||
@ -1470,14 +1470,14 @@ void HTMLBuilder::cwbr() {
|
|||||||
result += "</wbr>";
|
result += "</wbr>";
|
||||||
}
|
}
|
||||||
|
|
||||||
void HTMLBuilder::w(const std::string &val) {
|
void HTMLBuilder::w(const String &val) {
|
||||||
write_tag();
|
write_tag();
|
||||||
|
|
||||||
result += val;
|
result += val;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO!
|
//TODO!
|
||||||
void HTMLBuilder::we(const std::string &val) {
|
void HTMLBuilder::we(const String &val) {
|
||||||
printf("HTMLBuilder::write_excaped NYI!");
|
printf("HTMLBuilder::write_excaped NYI!");
|
||||||
|
|
||||||
write_tag();
|
write_tag();
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
#ifndef HTML_BUILDER_H
|
#ifndef HTML_BUILDER_H
|
||||||
#define HTML_BUILDER_H
|
#define HTML_BUILDER_H
|
||||||
|
|
||||||
#include <map>
|
#include "core/string.h"
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class Request;
|
class Request;
|
||||||
|
|
||||||
@ -12,27 +10,27 @@ class Request;
|
|||||||
class HTMLTag {
|
class HTMLTag {
|
||||||
public:
|
public:
|
||||||
bool simple;
|
bool simple;
|
||||||
std::string result;
|
String result;
|
||||||
|
|
||||||
HTMLTag *str(const std::string &str);
|
HTMLTag *str(const String &str);
|
||||||
HTMLTag *style(const std::string &val);
|
HTMLTag *style(const String &val);
|
||||||
HTMLTag *href(const std::string &val);
|
HTMLTag *href(const String &val);
|
||||||
HTMLTag *cls(const std::string &val);
|
HTMLTag *cls(const String &val);
|
||||||
HTMLTag *id(const std::string &val);
|
HTMLTag *id(const String &val);
|
||||||
HTMLTag *name(const std::string &val);
|
HTMLTag *name(const String &val);
|
||||||
HTMLTag *content(const std::string &val);
|
HTMLTag *content(const String &val);
|
||||||
HTMLTag *value(const std::string &val);
|
HTMLTag *value(const String &val);
|
||||||
HTMLTag *method(const std::string &val);
|
HTMLTag *method(const String &val);
|
||||||
HTMLTag *type(const std::string &val);
|
HTMLTag *type(const String &val);
|
||||||
HTMLTag *placeholder(const std::string &val);
|
HTMLTag *placeholder(const String &val);
|
||||||
HTMLTag *rel(const std::string &val);
|
HTMLTag *rel(const String &val);
|
||||||
HTMLTag *rel_stylesheet();
|
HTMLTag *rel_stylesheet();
|
||||||
HTMLTag *charset(const std::string &val);
|
HTMLTag *charset(const String &val);
|
||||||
HTMLTag *charset_utf_8();
|
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 *reset();
|
||||||
HTMLTag *close();
|
HTMLTag *close();
|
||||||
|
|
||||||
@ -43,11 +41,11 @@ public:
|
|||||||
|
|
||||||
class HTMLBuilder {
|
class HTMLBuilder {
|
||||||
public:
|
public:
|
||||||
std::string result;
|
String result;
|
||||||
|
|
||||||
void comment(const std::string &val);
|
void comment(const String &val);
|
||||||
HTMLTag *doctype();
|
HTMLTag *doctype();
|
||||||
void doctype(const std::string &val);
|
void doctype(const String &val);
|
||||||
|
|
||||||
HTMLTag *a();
|
HTMLTag *a();
|
||||||
HTMLTag *abbr();
|
HTMLTag *abbr();
|
||||||
@ -306,9 +304,9 @@ public:
|
|||||||
void cwbr();
|
void cwbr();
|
||||||
|
|
||||||
//write
|
//write
|
||||||
void w(const std::string &val);
|
void w(const String &val);
|
||||||
//write_escaped
|
//write_escaped
|
||||||
void we(const std::string &val);
|
void we(const String &val);
|
||||||
|
|
||||||
void write_tag();
|
void write_tag();
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ void UserController::handle_login_request_default(Request *request) {
|
|||||||
|
|
||||||
//this is probbaly not needed
|
//this is probbaly not needed
|
||||||
//it's ok for now as I need to test the validators more
|
//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);
|
_login_validator->validate(request, &errors);
|
||||||
for (int i = 0; i < errors.size(); ++i) {
|
for (int i = 0; i < errors.size(); ++i) {
|
||||||
data.error_str += errors[i] + "<br>";
|
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) {
|
if (request->get_method() == HTTP_METHOD_POST) {
|
||||||
|
|
||||||
std::vector<std::string> errors;
|
Vector<String> errors;
|
||||||
|
|
||||||
_registration_validator->validate(request, &errors);
|
_registration_validator->validate(request, &errors);
|
||||||
|
|
||||||
@ -310,7 +310,7 @@ void UserController::handle_settings_request(Ref<User> &user, Request *request)
|
|||||||
|
|
||||||
bool changed = false;
|
bool changed = false;
|
||||||
|
|
||||||
std::vector<std::string> errors;
|
Vector<String> errors;
|
||||||
|
|
||||||
bool valid = _profile_validator->validate(request, &errors);
|
bool valid = _profile_validator->validate(request, &errors);
|
||||||
|
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
#ifndef USER_CONTROLLER_H
|
#ifndef USER_CONTROLLER_H
|
||||||
#define USER_CONTROLLER_H
|
#define USER_CONTROLLER_H
|
||||||
|
|
||||||
|
#include "core/string.h"
|
||||||
|
#include "core/containers/vector.h"
|
||||||
|
|
||||||
#include "core/object.h"
|
#include "core/object.h"
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include "user.h"
|
#include "user.h"
|
||||||
|
|
||||||
class Request;
|
class Request;
|
||||||
|
Loading…
Reference in New Issue
Block a user