rcpp_framework/web/html/form_validator.h

200 lines
5.0 KiB
C
Raw Normal View History

2020-12-04 14:47:51 +01:00
#ifndef FORM_H
#define FORM_H
#include "core/string.h"
#include "core/containers/vector.h"
2021-08-20 18:00:50 +02:00
#include <ctype.h>
2020-12-04 14:47:51 +01:00
class Request;
2021-08-20 19:18:26 +02:00
class FormField;
2020-12-04 14:47:51 +01:00
2021-08-20 15:54:12 +02:00
class FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
2021-08-20 15:54:12 +02:00
FormFieldEntry();
virtual ~FormFieldEntry();
};
2021-08-20 17:26:56 +02:00
class FormExistsFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
2021-08-20 17:26:56 +02:00
FormExistsFieldEntry();
~FormExistsFieldEntry();
String not_exists_error;
2021-08-20 17:26:56 +02:00
};
class FormIntFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
2021-08-20 17:26:56 +02:00
FormIntFieldEntry();
~FormIntFieldEntry();
String not_int_error;
2021-08-20 17:26:56 +02:00
};
class FormFloatFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
2021-08-20 17:26:56 +02:00
FormFloatFieldEntry();
~FormFloatFieldEntry();
String not_float_error;
2021-08-20 17:26:56 +02:00
};
class FormAlphaFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
2021-08-20 17:26:56 +02:00
FormAlphaFieldEntry();
~FormAlphaFieldEntry();
String not_alpha_error;
2021-08-20 17:26:56 +02:00
};
class FormAlphaNumericFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
2021-08-20 17:26:56 +02:00
FormAlphaNumericFieldEntry();
~FormAlphaNumericFieldEntry();
String not_alpha_numeric_error;
2021-08-20 17:26:56 +02:00
};
class FormNeedsLowercaseCharacterFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
2021-08-20 17:26:56 +02:00
FormNeedsLowercaseCharacterFieldEntry();
~FormNeedsLowercaseCharacterFieldEntry();
String does_not_have_lowercase_error;
2021-08-20 17:26:56 +02:00
};
class FormNeedsUppercaseCharacterFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
2021-08-20 17:26:56 +02:00
FormNeedsUppercaseCharacterFieldEntry();
~FormNeedsUppercaseCharacterFieldEntry();
String does_not_have_uppercase_error;
2021-08-20 17:26:56 +02:00
};
class FormNeedsOtherCharacterFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
2021-08-20 17:26:56 +02:00
FormNeedsOtherCharacterFieldEntry();
~FormNeedsOtherCharacterFieldEntry();
String does_not_have_other_error;
2021-08-20 17:26:56 +02:00
};
class FormMinimumLengthFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
2021-08-20 17:26:56 +02:00
FormMinimumLengthFieldEntry();
~FormMinimumLengthFieldEntry();
2021-08-20 18:00:50 +02:00
int min_length;
2021-08-20 17:26:56 +02:00
String does_not_have_min_length_errorf;
String does_not_have_min_length_errors;
2021-08-20 17:26:56 +02:00
};
class FormMaximumLengthFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
2021-08-20 17:26:56 +02:00
FormMaximumLengthFieldEntry();
~FormMaximumLengthFieldEntry();
2021-08-20 18:00:50 +02:00
int max_length;
2021-08-20 17:26:56 +02:00
String does_not_have_max_length_errorf;
String does_not_have_max_length_errors;
2021-08-20 17:26:56 +02:00
};
class FormEmailFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
2021-08-20 17:26:56 +02:00
FormEmailFieldEntry();
~FormEmailFieldEntry();
String email_format_error;
2021-08-20 18:00:50 +02:00
};
class FormNeedToMatchOtherFieldEntry : public FormFieldEntry {
public:
virtual bool validate(Request *request, const FormField* field, const String &data, Vector<String> *errors);
2021-08-20 18:00:50 +02:00
FormNeedToMatchOtherFieldEntry();
~FormNeedToMatchOtherFieldEntry();
String other_field;
2021-08-20 18:00:50 +02:00
String does_not_match_error;
2021-08-20 17:26:56 +02:00
};
//FormField
2020-12-04 14:47:51 +01:00
class FormField {
public:
String name;
String human_name;
2020-12-04 14:47:51 +01:00
2021-08-20 20:32:09 +02:00
bool _ignore_if_not_exists;
bool _ignore_if_other_field_not_exists;
String _ignore_if_other_field_not_exist_field;
2021-08-20 20:32:09 +02:00
2021-08-20 18:00:50 +02:00
FormField *need_to_exist();
FormField *need_to_be_int();
FormField *need_to_be_float();
FormField *need_to_be_alpha();
FormField *need_to_be_alpha_numeric();
FormField *need_to_have_lowercase_character();
FormField *need_to_have_uppercase_character();
FormField *need_to_have_other_character();
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 String &other);
2021-08-20 20:32:09 +02:00
FormField *ignore_if_not_exists();
FormField *ignore_if_other_field_not_exists(const String &other);
2021-08-20 18:00:50 +02:00
2021-08-20 15:54:12 +02:00
void add_entry(FormFieldEntry *field);
2020-12-04 14:47:51 +01:00
bool validate(Request *request, Vector<String> *errors);
2020-12-04 14:47:51 +01:00
2021-08-20 15:54:12 +02:00
FormField();
virtual ~FormField();
2020-12-04 14:47:51 +01:00
Vector<FormFieldEntry *> fields;
2020-12-04 14:47:51 +01:00
};
2021-08-20 18:00:50 +02:00
//FormValidator
2021-08-20 15:54:12 +02:00
class FormValidator {
2020-12-04 14:47:51 +01:00
public:
bool validate(Request *request, Vector<String> *errors = nullptr);
2021-08-20 15:54:12 +02:00
void add_field(FormField *field);
FormField *new_field(const String &name, const String &human_name);
2020-12-04 14:47:51 +01:00
2021-08-20 15:54:12 +02:00
FormValidator();
virtual ~FormValidator();
2020-12-04 14:47:51 +01:00
Vector<FormField *> fields;
2020-12-04 14:47:51 +01:00
};
#endif