mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-14 04:57:21 +01:00
More improvements to form validation.
This commit is contained in:
parent
3bc7bd98c6
commit
f8ba0b26c5
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
//FormFieldEntry
|
//FormFieldEntry
|
||||||
|
|
||||||
bool FormFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
|
bool FormFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,10 +17,10 @@ FormFieldEntry::~FormFieldEntry() {
|
|||||||
|
|
||||||
//FormExistsFieldEntry
|
//FormExistsFieldEntry
|
||||||
|
|
||||||
bool FormExistsFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
|
bool FormExistsFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
|
||||||
if (data == "") {
|
if (data == "") {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(not_exists_error);
|
errors->push_back(field->human_name + not_exists_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -30,14 +30,14 @@ bool FormExistsFieldEntry::validate(Request *request, const std::string &field_n
|
|||||||
}
|
}
|
||||||
|
|
||||||
FormExistsFieldEntry::FormExistsFieldEntry() {
|
FormExistsFieldEntry::FormExistsFieldEntry() {
|
||||||
not_exists_error = "Form field need to exists!";
|
not_exists_error = " field need to exists!";
|
||||||
}
|
}
|
||||||
FormExistsFieldEntry::~FormExistsFieldEntry() {
|
FormExistsFieldEntry::~FormExistsFieldEntry() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//FormIntFieldEntry
|
//FormIntFieldEntry
|
||||||
|
|
||||||
bool FormIntFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
|
bool FormIntFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::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()) {
|
||||||
@ -46,7 +46,7 @@ bool FormIntFieldEntry::validate(Request *request, const std::string &field_name
|
|||||||
|
|
||||||
if (((!isdigit(data[0])) && (data[0] != '-') && (data[0] != '+'))) {
|
if (((!isdigit(data[0])) && (data[0] != '-') && (data[0] != '+'))) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(not_int_error);
|
errors->push_back(field->human_name + not_int_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -59,7 +59,7 @@ bool FormIntFieldEntry::validate(Request *request, const std::string &field_name
|
|||||||
|
|
||||||
if (!is_int) {
|
if (!is_int) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(not_int_error);
|
errors->push_back(field->human_name + not_int_error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +67,7 @@ bool FormIntFieldEntry::validate(Request *request, const std::string &field_name
|
|||||||
}
|
}
|
||||||
|
|
||||||
FormIntFieldEntry::FormIntFieldEntry() {
|
FormIntFieldEntry::FormIntFieldEntry() {
|
||||||
not_int_error = "Field needs to be an integer.";
|
not_int_error = " needs to be an integer.";
|
||||||
}
|
}
|
||||||
|
|
||||||
FormIntFieldEntry::~FormIntFieldEntry() {
|
FormIntFieldEntry::~FormIntFieldEntry() {
|
||||||
@ -75,7 +75,7 @@ FormIntFieldEntry::~FormIntFieldEntry() {
|
|||||||
|
|
||||||
//FormFloatFieldEntry
|
//FormFloatFieldEntry
|
||||||
|
|
||||||
bool FormFloatFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
|
bool FormFloatFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
|
||||||
if (data.empty()) {
|
if (data.empty()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -87,7 +87,7 @@ bool FormFloatFieldEntry::validate(Request *request, const std::string &field_na
|
|||||||
|
|
||||||
if (!is_float) {
|
if (!is_float) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(not_float_error);
|
errors->push_back(field->human_name + not_float_error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,18 +95,18 @@ bool FormFloatFieldEntry::validate(Request *request, const std::string &field_na
|
|||||||
}
|
}
|
||||||
|
|
||||||
FormFloatFieldEntry::FormFloatFieldEntry() {
|
FormFloatFieldEntry::FormFloatFieldEntry() {
|
||||||
not_float_error = "Field needs to be an floating point number.";
|
not_float_error = " needs to be an floating point number.";
|
||||||
}
|
}
|
||||||
FormFloatFieldEntry::~FormFloatFieldEntry() {
|
FormFloatFieldEntry::~FormFloatFieldEntry() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//FormAlphaFieldEntry
|
//FormAlphaFieldEntry
|
||||||
|
|
||||||
bool FormAlphaFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
|
bool FormAlphaFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::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) {
|
||||||
errors->push_back(not_alpha_error);
|
errors->push_back(field->human_name + not_alpha_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -117,18 +117,18 @@ bool FormAlphaFieldEntry::validate(Request *request, const std::string &field_na
|
|||||||
}
|
}
|
||||||
|
|
||||||
FormAlphaFieldEntry::FormAlphaFieldEntry() {
|
FormAlphaFieldEntry::FormAlphaFieldEntry() {
|
||||||
not_alpha_error = "Field needs to only contain caharcters.";
|
not_alpha_error = " needs to only contain caharcters.";
|
||||||
}
|
}
|
||||||
FormAlphaFieldEntry::~FormAlphaFieldEntry() {
|
FormAlphaFieldEntry::~FormAlphaFieldEntry() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//FormAlphaNumericFieldEntry
|
//FormAlphaNumericFieldEntry
|
||||||
|
|
||||||
bool FormAlphaNumericFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
|
bool FormAlphaNumericFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::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) {
|
||||||
errors->push_back(not_alpha_numeric_error);
|
errors->push_back(field->human_name + not_alpha_numeric_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -139,36 +139,37 @@ bool FormAlphaNumericFieldEntry::validate(Request *request, const std::string &f
|
|||||||
}
|
}
|
||||||
|
|
||||||
FormAlphaNumericFieldEntry::FormAlphaNumericFieldEntry() {
|
FormAlphaNumericFieldEntry::FormAlphaNumericFieldEntry() {
|
||||||
not_alpha_numeric_error = "Field needs to only contain caharcters of numbers.";
|
not_alpha_numeric_error = " needs to only contain caharcters of numbers.";
|
||||||
}
|
}
|
||||||
FormAlphaNumericFieldEntry::~FormAlphaNumericFieldEntry() {
|
FormAlphaNumericFieldEntry::~FormAlphaNumericFieldEntry() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//FormNeedsLowercaseCharacterFieldEntry
|
//FormNeedsLowercaseCharacterFieldEntry
|
||||||
|
|
||||||
bool FormNeedsLowercaseCharacterFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
|
bool FormNeedsLowercaseCharacterFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::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])) {
|
||||||
if (errors) {
|
|
||||||
errors->push_back(does_not_have_lowercase_error);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (errors) {
|
||||||
|
errors->push_back(field->human_name + does_not_have_lowercase_error);
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
FormNeedsLowercaseCharacterFieldEntry::FormNeedsLowercaseCharacterFieldEntry() {
|
FormNeedsLowercaseCharacterFieldEntry::FormNeedsLowercaseCharacterFieldEntry() {
|
||||||
does_not_have_lowercase_error = "Field needs at least one lowercase character!";
|
does_not_have_lowercase_error = " needs at least one lowercase character!";
|
||||||
}
|
}
|
||||||
FormNeedsLowercaseCharacterFieldEntry::~FormNeedsLowercaseCharacterFieldEntry() {
|
FormNeedsLowercaseCharacterFieldEntry::~FormNeedsLowercaseCharacterFieldEntry() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//FormNeedsUppercaseCharacterFieldEntry
|
//FormNeedsUppercaseCharacterFieldEntry
|
||||||
|
|
||||||
bool FormNeedsUppercaseCharacterFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
|
bool FormNeedsUppercaseCharacterFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::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;
|
||||||
@ -176,21 +177,21 @@ bool FormNeedsUppercaseCharacterFieldEntry::validate(Request *request, const std
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(does_not_have_uppercase_error);
|
errors->push_back(field->human_name + does_not_have_uppercase_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
FormNeedsUppercaseCharacterFieldEntry::FormNeedsUppercaseCharacterFieldEntry() {
|
FormNeedsUppercaseCharacterFieldEntry::FormNeedsUppercaseCharacterFieldEntry() {
|
||||||
does_not_have_uppercase_error = "Field needs at least one uppercase character!";
|
does_not_have_uppercase_error = " needs at least one uppercase character!";
|
||||||
}
|
}
|
||||||
FormNeedsUppercaseCharacterFieldEntry::~FormNeedsUppercaseCharacterFieldEntry() {
|
FormNeedsUppercaseCharacterFieldEntry::~FormNeedsUppercaseCharacterFieldEntry() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//FormNeedsOtherCharacterFieldEntry
|
//FormNeedsOtherCharacterFieldEntry
|
||||||
|
|
||||||
bool FormNeedsOtherCharacterFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
|
bool FormNeedsOtherCharacterFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::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;
|
||||||
@ -198,26 +199,26 @@ bool FormNeedsOtherCharacterFieldEntry::validate(Request *request, const std::st
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(does_not_have_other_error);
|
errors->push_back(field->human_name + does_not_have_other_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
FormNeedsOtherCharacterFieldEntry::FormNeedsOtherCharacterFieldEntry() {
|
FormNeedsOtherCharacterFieldEntry::FormNeedsOtherCharacterFieldEntry() {
|
||||||
does_not_have_other_error = "Field needs at least one other character!";
|
does_not_have_other_error = " needs at least one other character!";
|
||||||
}
|
}
|
||||||
FormNeedsOtherCharacterFieldEntry::~FormNeedsOtherCharacterFieldEntry() {
|
FormNeedsOtherCharacterFieldEntry::~FormNeedsOtherCharacterFieldEntry() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//FormMinimumLengthFieldEntry
|
//FormMinimumLengthFieldEntry
|
||||||
|
|
||||||
bool FormMinimumLengthFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
|
bool FormMinimumLengthFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
|
||||||
if (data.size() <= min_length) {
|
if (data.size() <= min_length) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
ss << 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());
|
errors->push_back(ss.str());
|
||||||
}
|
}
|
||||||
@ -229,7 +230,7 @@ bool FormMinimumLengthFieldEntry::validate(Request *request, const std::string &
|
|||||||
}
|
}
|
||||||
|
|
||||||
FormMinimumLengthFieldEntry::FormMinimumLengthFieldEntry() {
|
FormMinimumLengthFieldEntry::FormMinimumLengthFieldEntry() {
|
||||||
does_not_have_min_length_errorf = "Field needs at least ";
|
does_not_have_min_length_errorf = " needs at least ";
|
||||||
does_not_have_min_length_errors = " characters!";
|
does_not_have_min_length_errors = " characters!";
|
||||||
|
|
||||||
min_length = 5;
|
min_length = 5;
|
||||||
@ -239,12 +240,12 @@ FormMinimumLengthFieldEntry::~FormMinimumLengthFieldEntry() {
|
|||||||
|
|
||||||
//FormMaximumLengthFieldEntry
|
//FormMaximumLengthFieldEntry
|
||||||
|
|
||||||
bool FormMaximumLengthFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
|
bool FormMaximumLengthFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
|
||||||
if (data.size() >= max_length) {
|
if (data.size() >= max_length) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
ss << 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());
|
errors->push_back(ss.str());
|
||||||
}
|
}
|
||||||
@ -256,7 +257,7 @@ bool FormMaximumLengthFieldEntry::validate(Request *request, const std::string &
|
|||||||
}
|
}
|
||||||
|
|
||||||
FormMaximumLengthFieldEntry::FormMaximumLengthFieldEntry() {
|
FormMaximumLengthFieldEntry::FormMaximumLengthFieldEntry() {
|
||||||
does_not_have_max_length_errorf = "Field needs at most ";
|
does_not_have_max_length_errorf = " needs at most ";
|
||||||
does_not_have_max_length_errors = " characters!";
|
does_not_have_max_length_errors = " characters!";
|
||||||
|
|
||||||
max_length = 10;
|
max_length = 10;
|
||||||
@ -266,10 +267,10 @@ FormMaximumLengthFieldEntry::~FormMaximumLengthFieldEntry() {
|
|||||||
|
|
||||||
//FormEmailFieldEntry
|
//FormEmailFieldEntry
|
||||||
|
|
||||||
bool FormEmailFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
|
bool FormEmailFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
|
||||||
if (data.size() == 0) {
|
if (data.size() == 0) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(email_format_error);
|
errors->push_back(field->human_name + email_format_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -277,7 +278,7 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
|
|||||||
|
|
||||||
if (!isalpha(data[0])) {
|
if (!isalpha(data[0])) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(email_format_error);
|
errors->push_back(field->human_name + email_format_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -290,7 +291,7 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
|
|||||||
if (data[i] == '.') {
|
if (data[i] == '.') {
|
||||||
if (dot_pos != -1) {
|
if (dot_pos != -1) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(email_format_error);
|
errors->push_back(field->human_name + email_format_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -304,7 +305,7 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
|
|||||||
|
|
||||||
if (dot_pos == -1) {
|
if (dot_pos == -1) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(email_format_error);
|
errors->push_back(field->human_name + email_format_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -314,7 +315,7 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
|
|||||||
if (data[i] == '@') {
|
if (data[i] == '@') {
|
||||||
if (at_pos != -1) {
|
if (at_pos != -1) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(email_format_error);
|
errors->push_back(field->human_name + email_format_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -328,7 +329,7 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
|
|||||||
|
|
||||||
if (at_pos == -1) {
|
if (at_pos == -1) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(email_format_error);
|
errors->push_back(field->human_name + email_format_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -341,7 +342,7 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
|
|||||||
|
|
||||||
if (!isalnum(data[i])) {
|
if (!isalnum(data[i])) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(email_format_error);
|
errors->push_back(field->human_name + email_format_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -352,17 +353,17 @@ bool FormEmailFieldEntry::validate(Request *request, const std::string &field_na
|
|||||||
}
|
}
|
||||||
|
|
||||||
FormEmailFieldEntry::FormEmailFieldEntry() {
|
FormEmailFieldEntry::FormEmailFieldEntry() {
|
||||||
email_format_error = "Field is invalid!";
|
email_format_error = " is invalid!";
|
||||||
}
|
}
|
||||||
FormEmailFieldEntry::~FormEmailFieldEntry() {
|
FormEmailFieldEntry::~FormEmailFieldEntry() {
|
||||||
}
|
}
|
||||||
|
|
||||||
//FormNeedToMatchOtherFieldEntry
|
//FormNeedToMatchOtherFieldEntry
|
||||||
|
|
||||||
bool FormNeedToMatchOtherFieldEntry::validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors) {
|
bool FormNeedToMatchOtherFieldEntry::validate(Request *request, const FormField *field, const std::string &data, std::vector<std::string> *errors) {
|
||||||
if (data != request->get_parameter(other_field)) {
|
if (data != request->get_parameter(other_field)) {
|
||||||
if (errors) {
|
if (errors) {
|
||||||
errors->push_back(does_not_match_error + field_name + ".");
|
errors->push_back(field->human_name + does_not_match_error + field->name + ".");
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -372,7 +373,7 @@ bool FormNeedToMatchOtherFieldEntry::validate(Request *request, const std::strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
FormNeedToMatchOtherFieldEntry::FormNeedToMatchOtherFieldEntry() {
|
FormNeedToMatchOtherFieldEntry::FormNeedToMatchOtherFieldEntry() {
|
||||||
does_not_match_error = "Field does not match ";
|
does_not_match_error = " does not match ";
|
||||||
}
|
}
|
||||||
FormNeedToMatchOtherFieldEntry::~FormNeedToMatchOtherFieldEntry() {
|
FormNeedToMatchOtherFieldEntry::~FormNeedToMatchOtherFieldEntry() {
|
||||||
}
|
}
|
||||||
@ -428,7 +429,7 @@ FormField *FormField::need_minimum_length(const int min_length) {
|
|||||||
}
|
}
|
||||||
FormField *FormField::need_maximum_length(const int max_length) {
|
FormField *FormField::need_maximum_length(const int max_length) {
|
||||||
FormMaximumLengthFieldEntry *f = new FormMaximumLengthFieldEntry();
|
FormMaximumLengthFieldEntry *f = new FormMaximumLengthFieldEntry();
|
||||||
f->max_length =max_length;
|
f->max_length = max_length;
|
||||||
add_entry(f);
|
add_entry(f);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
@ -457,7 +458,7 @@ bool FormField::validate(Request *request, std::vector<std::string> *errors) {
|
|||||||
bool valid = true;
|
bool valid = true;
|
||||||
|
|
||||||
for (int i = 0; i < fields.size(); ++i) {
|
for (int i = 0; i < fields.size(); ++i) {
|
||||||
if (!fields[i]->validate(request, name, param, errors)) {
|
if (!fields[i]->validate(request, this, param, errors)) {
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -494,9 +495,10 @@ void FormValidator::add_field(FormField *field) {
|
|||||||
fields.push_back(field);
|
fields.push_back(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
FormField *FormValidator::new_field(const std::string &name) {
|
FormField *FormValidator::new_field(const std::string &name, const std::string &human_name) {
|
||||||
FormField *f = new FormField();
|
FormField *f = new FormField();
|
||||||
f->name = name;
|
f->name = name;
|
||||||
|
f->human_name = human_name;
|
||||||
|
|
||||||
fields.push_back(f);
|
fields.push_back(f);
|
||||||
|
|
||||||
|
@ -7,10 +7,11 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class Request;
|
class Request;
|
||||||
|
class FormField;
|
||||||
|
|
||||||
class FormFieldEntry {
|
class FormFieldEntry {
|
||||||
public:
|
public:
|
||||||
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
|
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
|
||||||
|
|
||||||
FormFieldEntry();
|
FormFieldEntry();
|
||||||
virtual ~FormFieldEntry();
|
virtual ~FormFieldEntry();
|
||||||
@ -18,7 +19,7 @@ public:
|
|||||||
|
|
||||||
class FormExistsFieldEntry : public FormFieldEntry {
|
class FormExistsFieldEntry : public FormFieldEntry {
|
||||||
public:
|
public:
|
||||||
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
|
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
|
||||||
|
|
||||||
FormExistsFieldEntry();
|
FormExistsFieldEntry();
|
||||||
~FormExistsFieldEntry();
|
~FormExistsFieldEntry();
|
||||||
@ -28,7 +29,7 @@ public:
|
|||||||
|
|
||||||
class FormIntFieldEntry : public FormFieldEntry {
|
class FormIntFieldEntry : public FormFieldEntry {
|
||||||
public:
|
public:
|
||||||
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
|
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
|
||||||
|
|
||||||
FormIntFieldEntry();
|
FormIntFieldEntry();
|
||||||
~FormIntFieldEntry();
|
~FormIntFieldEntry();
|
||||||
@ -38,7 +39,7 @@ public:
|
|||||||
|
|
||||||
class FormFloatFieldEntry : public FormFieldEntry {
|
class FormFloatFieldEntry : public FormFieldEntry {
|
||||||
public:
|
public:
|
||||||
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
|
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
|
||||||
|
|
||||||
FormFloatFieldEntry();
|
FormFloatFieldEntry();
|
||||||
~FormFloatFieldEntry();
|
~FormFloatFieldEntry();
|
||||||
@ -48,7 +49,7 @@ public:
|
|||||||
|
|
||||||
class FormAlphaFieldEntry : public FormFieldEntry {
|
class FormAlphaFieldEntry : public FormFieldEntry {
|
||||||
public:
|
public:
|
||||||
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
|
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
|
||||||
|
|
||||||
FormAlphaFieldEntry();
|
FormAlphaFieldEntry();
|
||||||
~FormAlphaFieldEntry();
|
~FormAlphaFieldEntry();
|
||||||
@ -58,7 +59,7 @@ public:
|
|||||||
|
|
||||||
class FormAlphaNumericFieldEntry : public FormFieldEntry {
|
class FormAlphaNumericFieldEntry : public FormFieldEntry {
|
||||||
public:
|
public:
|
||||||
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
|
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
|
||||||
|
|
||||||
FormAlphaNumericFieldEntry();
|
FormAlphaNumericFieldEntry();
|
||||||
~FormAlphaNumericFieldEntry();
|
~FormAlphaNumericFieldEntry();
|
||||||
@ -68,7 +69,7 @@ public:
|
|||||||
|
|
||||||
class FormNeedsLowercaseCharacterFieldEntry : public FormFieldEntry {
|
class FormNeedsLowercaseCharacterFieldEntry : public FormFieldEntry {
|
||||||
public:
|
public:
|
||||||
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
|
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
|
||||||
|
|
||||||
FormNeedsLowercaseCharacterFieldEntry();
|
FormNeedsLowercaseCharacterFieldEntry();
|
||||||
~FormNeedsLowercaseCharacterFieldEntry();
|
~FormNeedsLowercaseCharacterFieldEntry();
|
||||||
@ -78,7 +79,7 @@ public:
|
|||||||
|
|
||||||
class FormNeedsUppercaseCharacterFieldEntry : public FormFieldEntry {
|
class FormNeedsUppercaseCharacterFieldEntry : public FormFieldEntry {
|
||||||
public:
|
public:
|
||||||
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
|
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
|
||||||
|
|
||||||
FormNeedsUppercaseCharacterFieldEntry();
|
FormNeedsUppercaseCharacterFieldEntry();
|
||||||
~FormNeedsUppercaseCharacterFieldEntry();
|
~FormNeedsUppercaseCharacterFieldEntry();
|
||||||
@ -88,7 +89,7 @@ public:
|
|||||||
|
|
||||||
class FormNeedsOtherCharacterFieldEntry : public FormFieldEntry {
|
class FormNeedsOtherCharacterFieldEntry : public FormFieldEntry {
|
||||||
public:
|
public:
|
||||||
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
|
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
|
||||||
|
|
||||||
FormNeedsOtherCharacterFieldEntry();
|
FormNeedsOtherCharacterFieldEntry();
|
||||||
~FormNeedsOtherCharacterFieldEntry();
|
~FormNeedsOtherCharacterFieldEntry();
|
||||||
@ -98,7 +99,7 @@ public:
|
|||||||
|
|
||||||
class FormMinimumLengthFieldEntry : public FormFieldEntry {
|
class FormMinimumLengthFieldEntry : public FormFieldEntry {
|
||||||
public:
|
public:
|
||||||
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
|
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
|
||||||
|
|
||||||
FormMinimumLengthFieldEntry();
|
FormMinimumLengthFieldEntry();
|
||||||
~FormMinimumLengthFieldEntry();
|
~FormMinimumLengthFieldEntry();
|
||||||
@ -111,7 +112,7 @@ public:
|
|||||||
|
|
||||||
class FormMaximumLengthFieldEntry : public FormFieldEntry {
|
class FormMaximumLengthFieldEntry : public FormFieldEntry {
|
||||||
public:
|
public:
|
||||||
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
|
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
|
||||||
|
|
||||||
FormMaximumLengthFieldEntry();
|
FormMaximumLengthFieldEntry();
|
||||||
~FormMaximumLengthFieldEntry();
|
~FormMaximumLengthFieldEntry();
|
||||||
@ -124,7 +125,7 @@ public:
|
|||||||
|
|
||||||
class FormEmailFieldEntry : public FormFieldEntry {
|
class FormEmailFieldEntry : public FormFieldEntry {
|
||||||
public:
|
public:
|
||||||
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
|
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
|
||||||
|
|
||||||
FormEmailFieldEntry();
|
FormEmailFieldEntry();
|
||||||
~FormEmailFieldEntry();
|
~FormEmailFieldEntry();
|
||||||
@ -134,7 +135,7 @@ public:
|
|||||||
|
|
||||||
class FormNeedToMatchOtherFieldEntry : public FormFieldEntry {
|
class FormNeedToMatchOtherFieldEntry : public FormFieldEntry {
|
||||||
public:
|
public:
|
||||||
virtual bool validate(Request *request, const std::string &field_name, const std::string &data, std::vector<std::string> *errors);
|
virtual bool validate(Request *request, const FormField* field, const std::string &data, std::vector<std::string> *errors);
|
||||||
|
|
||||||
FormNeedToMatchOtherFieldEntry();
|
FormNeedToMatchOtherFieldEntry();
|
||||||
~FormNeedToMatchOtherFieldEntry();
|
~FormNeedToMatchOtherFieldEntry();
|
||||||
@ -149,6 +150,7 @@ public:
|
|||||||
class FormField {
|
class FormField {
|
||||||
public:
|
public:
|
||||||
std::string name;
|
std::string name;
|
||||||
|
std::string human_name;
|
||||||
|
|
||||||
FormField *need_to_exist();
|
FormField *need_to_exist();
|
||||||
FormField *need_to_be_int();
|
FormField *need_to_be_int();
|
||||||
@ -180,7 +182,7 @@ public:
|
|||||||
bool validate(Request *request, std::vector<std::string> *errors = nullptr);
|
bool validate(Request *request, std::vector<std::string> *errors = nullptr);
|
||||||
|
|
||||||
void add_field(FormField *field);
|
void add_field(FormField *field);
|
||||||
FormField *new_field(const std::string &name);
|
FormField *new_field(const std::string &name, const std::string &human_name);
|
||||||
|
|
||||||
FormValidator();
|
FormValidator();
|
||||||
virtual ~FormValidator();
|
virtual ~FormValidator();
|
||||||
|
@ -25,6 +25,8 @@ DBBasedUserManager::DBBasedUserManager() :
|
|||||||
UserManager() {
|
UserManager() {
|
||||||
|
|
||||||
printf("Using DBBasedUserManager.\n");
|
printf("Using DBBasedUserManager.\n");
|
||||||
|
|
||||||
|
User::create_validators();
|
||||||
}
|
}
|
||||||
|
|
||||||
DBBasedUserManager::~DBBasedUserManager() {
|
DBBasedUserManager::~DBBasedUserManager() {
|
||||||
|
@ -137,6 +137,16 @@ void User::handle_login_request_default(Request *request) {
|
|||||||
std::string pass_val = "";
|
std::string pass_val = "";
|
||||||
|
|
||||||
if (request->get_method() == HTTP_METHOD_POST) {
|
if (request->get_method() == HTTP_METHOD_POST) {
|
||||||
|
|
||||||
|
//this is probbaly not needed
|
||||||
|
//it's ok for now as I need to test the validators more
|
||||||
|
std::vector<std::string> errors;
|
||||||
|
_login_validator->validate(request, &errors);
|
||||||
|
for (int i = 0; i < errors.size(); ++i) {
|
||||||
|
error_str += errors[i] + "<br>";
|
||||||
|
}
|
||||||
|
//not needed end
|
||||||
|
|
||||||
uname_val = request->get_parameter("username");
|
uname_val = request->get_parameter("username");
|
||||||
pass_val = request->get_parameter("password");
|
pass_val = request->get_parameter("password");
|
||||||
|
|
||||||
@ -214,6 +224,15 @@ void User::handle_register_request_default(Request *request) {
|
|||||||
std::string pass_check_val = "";
|
std::string pass_check_val = "";
|
||||||
|
|
||||||
if (request->get_method() == HTTP_METHOD_POST) {
|
if (request->get_method() == HTTP_METHOD_POST) {
|
||||||
|
|
||||||
|
std::vector<std::string> errors;
|
||||||
|
|
||||||
|
_registration_validator->validate(request, &errors);
|
||||||
|
|
||||||
|
for (int i = 0; i < errors.size(); ++i) {
|
||||||
|
error_str += errors[i] + "<br>";
|
||||||
|
}
|
||||||
|
|
||||||
uname_val = request->get_parameter("username");
|
uname_val = request->get_parameter("username");
|
||||||
email_val = request->get_parameter("email");
|
email_val = request->get_parameter("email");
|
||||||
pass_val = request->get_parameter("password");
|
pass_val = request->get_parameter("password");
|
||||||
@ -389,29 +408,33 @@ void User::handle_delete_request(Request *request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void User::create_validators() {
|
void User::create_validators() {
|
||||||
//Login
|
if (!_login_validator) {
|
||||||
_login_validator = new FormValidator();
|
//Login
|
||||||
|
_login_validator = new FormValidator();
|
||||||
|
|
||||||
_login_validator->new_field("username")->need_to_exist()->need_to_be_alpha_numeric()->need_minimum_length(5)->need_maximum_length(20);
|
_login_validator->new_field("username", "Username")->need_to_exist()->need_to_be_alpha_numeric()->need_minimum_length(5)->need_maximum_length(20);
|
||||||
FormField *pw = _login_validator->new_field("password");
|
FormField *pw = _login_validator->new_field("password", "Password");
|
||||||
pw->need_to_exist();
|
pw->need_to_exist();
|
||||||
pw->need_to_have_lowercase_character()->need_to_have_uppercase_character();
|
pw->need_to_have_lowercase_character()->need_to_have_uppercase_character();
|
||||||
pw->need_minimum_length(5);
|
pw->need_minimum_length(5);
|
||||||
|
}
|
||||||
|
|
||||||
//Registration
|
if (!_registration_validator) {
|
||||||
_registration_validator = new FormValidator();
|
//Registration
|
||||||
|
_registration_validator = new FormValidator();
|
||||||
|
|
||||||
_registration_validator->new_field("username")->need_to_exist()->need_to_be_alpha_numeric()->need_minimum_length(5)->need_maximum_length(20);
|
_registration_validator->new_field("username", "Username")->need_to_exist()->need_to_be_alpha_numeric()->need_minimum_length(5)->need_maximum_length(20);
|
||||||
_registration_validator->new_field("email")->need_to_exist()->need_to_be_email();
|
_registration_validator->new_field("email", "Email")->need_to_exist()->need_to_be_email();
|
||||||
|
|
||||||
pw = _registration_validator->new_field("password");
|
FormField *pw = _registration_validator->new_field("password", "Password");
|
||||||
pw->need_to_exist();
|
pw->need_to_exist();
|
||||||
pw->need_to_have_lowercase_character()->need_to_have_uppercase_character();
|
pw->need_to_have_lowercase_character()->need_to_have_uppercase_character();
|
||||||
pw->need_minimum_length(5);
|
pw->need_minimum_length(5);
|
||||||
|
|
||||||
_registration_validator->new_field("password_check")->need_to_match("password");
|
_registration_validator->new_field("password_check", "Password check")->need_to_match("password");
|
||||||
|
|
||||||
_registration_validator->new_field("email")->need_to_exist()->need_to_be_email();
|
_registration_validator->new_field("email", "Email")->need_to_exist()->need_to_be_email();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
User::User() :
|
User::User() :
|
||||||
@ -431,3 +454,6 @@ User::User() :
|
|||||||
User::~User() {
|
User::~User() {
|
||||||
unregister_sessions();
|
unregister_sessions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FormValidator *User::_login_validator = nullptr;
|
||||||
|
FormValidator *User::_registration_validator = nullptr;
|
@ -48,7 +48,7 @@ public:
|
|||||||
virtual void handle_logout_request(Request *request);
|
virtual void handle_logout_request(Request *request);
|
||||||
virtual void handle_delete_request(Request *request);
|
virtual void handle_delete_request(Request *request);
|
||||||
|
|
||||||
virtual void create_validators();
|
static void create_validators();
|
||||||
|
|
||||||
void register_sessions();
|
void register_sessions();
|
||||||
void unregister_sessions();
|
void unregister_sessions();
|
||||||
@ -56,8 +56,8 @@ public:
|
|||||||
User();
|
User();
|
||||||
~User();
|
~User();
|
||||||
|
|
||||||
FormValidator *_login_validator;
|
static FormValidator *_login_validator;
|
||||||
FormValidator *_registration_validator;
|
static FormValidator *_registration_validator;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user