More work on the form vlaidator.

This commit is contained in:
Relintai 2021-08-20 20:32:09 +02:00
parent 1b4ecafa2c
commit 7e8a7671e4
2 changed files with 36 additions and 2 deletions

View File

@ -214,7 +214,7 @@ FormNeedsOtherCharacterFieldEntry::~FormNeedsOtherCharacterFieldEntry() {
//FormMinimumLengthFieldEntry
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) {
std::stringstream ss;
@ -241,7 +241,7 @@ FormMinimumLengthFieldEntry::~FormMinimumLengthFieldEntry() {
//FormMaximumLengthFieldEntry
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) {
std::stringstream ss;
@ -448,6 +448,19 @@ FormField *FormField::need_to_match(const std::string &other) {
return this;
}
FormField *FormField::ignore_if_not_exists() {
_ignore_if_not_exists = true;
return this;
}
FormField *FormField::ignore_if_other_field_not_exists(const std::string &other) {
_ignore_if_other_field_not_exists = true;
_ignore_if_other_field_not_exist_field = other;
return this;
}
void FormField::add_entry(FormFieldEntry *field) {
fields.push_back(field);
}
@ -455,6 +468,18 @@ void FormField::add_entry(FormFieldEntry *field) {
bool FormField::validate(Request *request, std::vector<std::string> *errors) {
std::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);
if (op == "") {
return true;
}
}
bool valid = true;
for (int i = 0; i < fields.size(); ++i) {
@ -467,6 +492,8 @@ bool FormField::validate(Request *request, std::vector<std::string> *errors) {
}
FormField::FormField() {
_ignore_if_not_exists = false;
_ignore_if_other_field_not_exists = false;
}
FormField::~FormField() {
for (int i = 0; i < fields.size(); ++i) {

View File

@ -152,6 +152,11 @@ public:
std::string name;
std::string human_name;
bool _ignore_if_not_exists;
bool _ignore_if_other_field_not_exists;
std::string _ignore_if_other_field_not_exist_field;
FormField *need_to_exist();
FormField *need_to_be_int();
FormField *need_to_be_float();
@ -164,6 +169,8 @@ public:
FormField *need_maximum_length(const int max_length);
FormField *need_to_be_email();
FormField *need_to_match(const std::string &other);
FormField *ignore_if_not_exists();
FormField *ignore_if_other_field_not_exists(const std::string &other);
void add_entry(FormFieldEntry *field);