2022-07-03 20:27:22 +02:00
|
|
|
#ifndef HTTP_FORM_VALIDATOR_H
|
|
|
|
#define HTTP_FORM_VALIDATOR_H
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-07-03 20:27:22 +02:00
|
|
|
#include "core/ustring.h"
|
|
|
|
#include "core/vector.h"
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-07-03 20:27:22 +02:00
|
|
|
#include "core/resource.h"
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
class Request;
|
|
|
|
class FormField;
|
2022-07-03 20:27:22 +02:00
|
|
|
class WebServerRequest;
|
|
|
|
|
|
|
|
class FormFieldEntry : public Resource {
|
|
|
|
GDCLASS(FormFieldEntry, Resource);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
|
|
|
virtual PoolStringArray _validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormFieldEntry();
|
2022-07-03 20:27:22 +02:00
|
|
|
~FormFieldEntry();
|
2022-06-25 01:55:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class FormExistsFieldEntry : public FormFieldEntry {
|
2022-07-03 20:27:22 +02:00
|
|
|
GDCLASS(FormExistsFieldEntry, FormFieldEntry);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray _validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormExistsFieldEntry();
|
|
|
|
~FormExistsFieldEntry();
|
|
|
|
|
|
|
|
String not_exists_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FormIntFieldEntry : public FormFieldEntry {
|
2022-07-03 20:27:22 +02:00
|
|
|
GDCLASS(FormIntFieldEntry, FormFieldEntry);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray _validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormIntFieldEntry();
|
|
|
|
~FormIntFieldEntry();
|
|
|
|
|
|
|
|
String not_int_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FormFloatFieldEntry : public FormFieldEntry {
|
2022-07-03 20:27:22 +02:00
|
|
|
GDCLASS(FormFloatFieldEntry, FormFieldEntry);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray _validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormFloatFieldEntry();
|
|
|
|
~FormFloatFieldEntry();
|
|
|
|
|
|
|
|
String not_float_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FormAlphaFieldEntry : public FormFieldEntry {
|
2022-07-03 20:27:22 +02:00
|
|
|
GDCLASS(FormAlphaFieldEntry, FormFieldEntry);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray _validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormAlphaFieldEntry();
|
|
|
|
~FormAlphaFieldEntry();
|
|
|
|
|
|
|
|
String not_alpha_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FormAlphaNumericFieldEntry : public FormFieldEntry {
|
2022-07-03 20:27:22 +02:00
|
|
|
GDCLASS(FormAlphaNumericFieldEntry, FormFieldEntry);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray _validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormAlphaNumericFieldEntry();
|
|
|
|
~FormAlphaNumericFieldEntry();
|
|
|
|
|
|
|
|
String not_alpha_numeric_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FormNeedsLowercaseCharacterFieldEntry : public FormFieldEntry {
|
2022-07-03 20:27:22 +02:00
|
|
|
GDCLASS(FormNeedsLowercaseCharacterFieldEntry, FormFieldEntry);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray _validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormNeedsLowercaseCharacterFieldEntry();
|
|
|
|
~FormNeedsLowercaseCharacterFieldEntry();
|
|
|
|
|
|
|
|
String does_not_have_lowercase_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FormNeedsUppercaseCharacterFieldEntry : public FormFieldEntry {
|
2022-07-03 20:27:22 +02:00
|
|
|
GDCLASS(FormNeedsUppercaseCharacterFieldEntry, FormFieldEntry);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray _validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormNeedsUppercaseCharacterFieldEntry();
|
|
|
|
~FormNeedsUppercaseCharacterFieldEntry();
|
|
|
|
|
|
|
|
String does_not_have_uppercase_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FormNeedsOtherCharacterFieldEntry : public FormFieldEntry {
|
2022-07-03 20:27:22 +02:00
|
|
|
GDCLASS(FormNeedsOtherCharacterFieldEntry, FormFieldEntry);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray _validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormNeedsOtherCharacterFieldEntry();
|
|
|
|
~FormNeedsOtherCharacterFieldEntry();
|
|
|
|
|
|
|
|
String does_not_have_other_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FormMinimumLengthFieldEntry : public FormFieldEntry {
|
2022-07-03 20:27:22 +02:00
|
|
|
GDCLASS(FormMinimumLengthFieldEntry, FormFieldEntry);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray _validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormMinimumLengthFieldEntry();
|
|
|
|
~FormMinimumLengthFieldEntry();
|
|
|
|
|
|
|
|
int min_length;
|
|
|
|
|
|
|
|
String does_not_have_min_length_errorf;
|
|
|
|
String does_not_have_min_length_errors;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FormMaximumLengthFieldEntry : public FormFieldEntry {
|
2022-07-03 20:27:22 +02:00
|
|
|
GDCLASS(FormMaximumLengthFieldEntry, FormFieldEntry);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray _validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormMaximumLengthFieldEntry();
|
|
|
|
~FormMaximumLengthFieldEntry();
|
|
|
|
|
|
|
|
int max_length;
|
|
|
|
|
|
|
|
String does_not_have_max_length_errorf;
|
|
|
|
String does_not_have_max_length_errors;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FormEmailFieldEntry : public FormFieldEntry {
|
2022-07-03 20:27:22 +02:00
|
|
|
GDCLASS(FormEmailFieldEntry, FormFieldEntry);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray _validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormEmailFieldEntry();
|
|
|
|
~FormEmailFieldEntry();
|
|
|
|
|
|
|
|
String email_format_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FormNeedToMatchOtherFieldEntry : public FormFieldEntry {
|
2022-07-03 20:27:22 +02:00
|
|
|
GDCLASS(FormNeedToMatchOtherFieldEntry, FormFieldEntry);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray _validate(Ref<WebServerRequest> request, const Ref<FormField> &field, const String &data);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormNeedToMatchOtherFieldEntry();
|
|
|
|
~FormNeedToMatchOtherFieldEntry();
|
|
|
|
|
|
|
|
String other_field;
|
|
|
|
|
|
|
|
String does_not_match_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
//FormField
|
|
|
|
|
2022-07-03 20:27:22 +02:00
|
|
|
class FormField : public Resource {
|
|
|
|
GDCLASS(FormField, Resource);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
|
|
|
String name;
|
2022-07-03 20:27:22 +02:00
|
|
|
String human_name;
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
bool _ignore_if_not_exists;
|
|
|
|
|
|
|
|
bool _ignore_if_other_field_not_exists;
|
|
|
|
String _ignore_if_other_field_not_exist_field;
|
|
|
|
|
2022-07-03 20:27:22 +02:00
|
|
|
Ref<FormField> need_to_exist();
|
|
|
|
Ref<FormField> need_to_be_int();
|
|
|
|
Ref<FormField> need_to_be_float();
|
|
|
|
Ref<FormField> need_to_be_alpha();
|
|
|
|
Ref<FormField> need_to_be_alpha_numeric();
|
|
|
|
Ref<FormField> need_to_have_lowercase_character();
|
|
|
|
Ref<FormField> need_to_have_uppercase_character();
|
|
|
|
Ref<FormField> need_to_have_other_character();
|
|
|
|
Ref<FormField> need_minimum_length(const int min_length);
|
|
|
|
Ref<FormField> need_maximum_length(const int max_length);
|
|
|
|
Ref<FormField> need_to_be_email();
|
|
|
|
Ref<FormField> need_to_match(const String &other);
|
|
|
|
Ref<FormField> ignore_if_not_exists();
|
|
|
|
Ref<FormField> ignore_if_other_field_not_exists(const String &other);
|
|
|
|
|
|
|
|
void add_entry(const Ref<FormFieldEntry> &field);
|
|
|
|
|
|
|
|
PoolStringArray validate(const Ref<WebServerRequest> &request);
|
|
|
|
virtual PoolStringArray _validate(Ref<WebServerRequest> request);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormField();
|
2022-07-03 20:27:22 +02:00
|
|
|
~FormField();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-07-03 20:27:22 +02:00
|
|
|
Vector<Ref<FormFieldEntry>> fields;
|
2022-06-25 01:55:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
//FormValidator
|
|
|
|
|
2022-07-03 20:27:22 +02:00
|
|
|
class FormValidator : public Resource {
|
|
|
|
GDCLASS(FormValidator, Resource);
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
public:
|
2022-07-03 20:27:22 +02:00
|
|
|
PoolStringArray validate(const Ref<WebServerRequest> &request);
|
|
|
|
virtual PoolStringArray _validate(Ref<WebServerRequest> request);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-07-03 20:27:22 +02:00
|
|
|
void add_field(const Ref<FormField> &field);
|
|
|
|
Ref<FormField> new_field(const String &name, const String &human_name);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
FormValidator();
|
2022-07-03 20:27:22 +02:00
|
|
|
~FormValidator();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-07-03 20:27:22 +02:00
|
|
|
protected:
|
|
|
|
Vector<Ref<FormField>> fields;
|
2022-06-25 01:55:54 +02:00
|
|
|
};
|
|
|
|
|
2022-07-03 20:27:22 +02:00
|
|
|
#endif
|