diff --git a/modules/web/html/form_validator.cpp b/modules/web/html/form_validator.cpp index 8b6caefd6..58f7cd356 100644 --- a/modules/web/html/form_validator.cpp +++ b/modules/web/html/form_validator.cpp @@ -24,7 +24,7 @@ PoolStringArray FormExistsFieldEntry::_validate(Ref request, c PoolStringArray errors; if (data == "") { - errors.push_back(field->human_name + not_exists_error); + errors.push_back(field->get_human_name() + not_exists_error); } return errors; @@ -46,7 +46,7 @@ PoolStringArray FormIntFieldEntry::_validate(Ref request, cons } if (!data.is_valid_integer()) { - errors.push_back(field->human_name + not_int_error); + errors.push_back(field->get_human_name() + not_int_error); } return errors; @@ -69,7 +69,7 @@ PoolStringArray FormFloatFieldEntry::_validate(Ref request, co } if (!data.is_valid_float()) { - errors.push_back(field->human_name + not_float_error); + errors.push_back(field->get_human_name() + not_float_error); } return errors; @@ -88,7 +88,7 @@ PoolStringArray FormAlphaFieldEntry::_validate(Ref request, co for (int i = 0; i < data.size(); ++i) { if (!isalpha(data[i])) { - errors.push_back(field->human_name + not_alpha_error); + errors.push_back(field->get_human_name() + not_alpha_error); return errors; } @@ -110,7 +110,7 @@ PoolStringArray FormAlphaNumericFieldEntry::_validate(Ref requ for (int i = 0; i < data.size(); ++i) { if (!isalnum(data[i])) { - errors.push_back(field->human_name + not_alpha_numeric_error); + errors.push_back(field->get_human_name() + not_alpha_numeric_error); return errors; } @@ -136,7 +136,7 @@ PoolStringArray FormNeedsLowercaseCharacterFieldEntry::_validate(Refhuman_name + does_not_have_lowercase_error); + errors.push_back(field->get_human_name() + does_not_have_lowercase_error); return errors; } @@ -157,7 +157,7 @@ PoolStringArray FormNeedsUppercaseCharacterFieldEntry::_validate(Refhuman_name + does_not_have_uppercase_error); + errors.push_back(field->get_human_name() + does_not_have_uppercase_error); return errors; } @@ -178,7 +178,7 @@ PoolStringArray FormNeedsOtherCharacterFieldEntry::_validate(Refhuman_name + does_not_have_other_error); + errors.push_back(field->get_human_name() + does_not_have_other_error); return errors; } @@ -194,7 +194,7 @@ FormNeedsOtherCharacterFieldEntry::~FormNeedsOtherCharacterFieldEntry() { PoolStringArray FormMinimumLengthFieldEntry::_validate(Ref request, const Ref &field, const String &data) { PoolStringArray errors; if (data.size() < min_length) { - errors.push_back(field->human_name + does_not_have_min_length_errorf + itos(min_length) + does_not_have_min_length_errors); + errors.push_back(field->get_human_name() + does_not_have_min_length_errorf + itos(min_length) + does_not_have_min_length_errors); } return errors; @@ -215,7 +215,7 @@ PoolStringArray FormMaximumLengthFieldEntry::_validate(Ref req PoolStringArray errors; if (data.size() > max_length) { - errors.push_back(field->human_name + does_not_have_max_length_errorf + itos(max_length) + does_not_have_max_length_errors); + errors.push_back(field->get_human_name() + does_not_have_max_length_errorf + itos(max_length) + does_not_have_max_length_errors); } return errors; @@ -236,13 +236,13 @@ PoolStringArray FormEmailFieldEntry::_validate(Ref request, co PoolStringArray errors; if (data.size() == 0) { - errors.push_back(field->human_name + email_format_error); + errors.push_back(field->get_human_name() + email_format_error); return errors; } if (!isalpha(data[0])) { - errors.push_back(field->human_name + email_format_error); + errors.push_back(field->get_human_name() + email_format_error); return errors; } @@ -253,7 +253,7 @@ PoolStringArray FormEmailFieldEntry::_validate(Ref request, co for (int i = 0; i < data.size(); ++i) { if (data[i] == '.') { if (dot_pos != -1) { - errors.push_back(field->human_name + email_format_error); + errors.push_back(field->get_human_name() + email_format_error); return errors; } @@ -265,7 +265,7 @@ PoolStringArray FormEmailFieldEntry::_validate(Ref request, co } if (dot_pos == -1) { - errors.push_back(field->human_name + email_format_error); + errors.push_back(field->get_human_name() + email_format_error); return errors; } @@ -273,7 +273,7 @@ PoolStringArray FormEmailFieldEntry::_validate(Ref request, co for (int i = 0; i < data.size(); ++i) { if (data[i] == '@') { if (at_pos != -1) { - errors.push_back(field->human_name + email_format_error); + errors.push_back(field->get_human_name() + email_format_error); return errors; } @@ -285,7 +285,7 @@ PoolStringArray FormEmailFieldEntry::_validate(Ref request, co } if (at_pos == -1) { - errors.push_back(field->human_name + email_format_error); + errors.push_back(field->get_human_name() + email_format_error); return errors; } @@ -296,7 +296,7 @@ PoolStringArray FormEmailFieldEntry::_validate(Ref request, co } if (!isalnum(data[i])) { - errors.push_back(field->human_name + email_format_error); + errors.push_back(field->get_human_name() + email_format_error); return errors; } @@ -317,7 +317,7 @@ PoolStringArray FormNeedToMatchOtherFieldEntry::_validate(Ref PoolStringArray errors; if (data != request->get_parameter(other_field)) { - errors.push_back(field->human_name + does_not_match_error + field->name + "."); + errors.push_back(field->get_human_name() + does_not_match_error + field->get_field_name() + "."); } return errors; @@ -331,6 +331,80 @@ FormNeedToMatchOtherFieldEntry::~FormNeedToMatchOtherFieldEntry() { //FormField +String FormField::get_field_name() const { + return _field_name; +} +void FormField::set_field_name(const String &val) { + _field_name = val; +} + +String FormField::get_human_name() const { + return _human_name; +} +void FormField::set_human_name(const String &val) { + _human_name = val; +} + +bool FormField::get_ignore_if_not_exists() const { + return _ignore_if_not_exists; +} +void FormField::set_ignore_if_not_exists(const bool &val) { + _ignore_if_not_exists = val; +} + +bool FormField::get_ignore_if_other_field_not_exists() const { + return _ignore_if_other_field_not_exists; +} +void FormField::set_ignore_if_other_field_not_exists(const bool &val) { + _ignore_if_other_field_not_exists = val; +} + +String FormField::get_ignore_if_other_field_not_exist_field() const { + return _ignore_if_other_field_not_exist_field; +} +void FormField::set_ignore_if_other_field_not_exist_field(const String &val) { + _ignore_if_other_field_not_exist_field = val; +} + +void FormField::add_entry(const Ref &field) { + _entries.push_back(field); +} + +Ref FormField::get_entry(const int index) { + ERR_FAIL_INDEX_V(index, _entries.size(), Ref()); + + return _entries[index]; +} + +void FormField::remove_entry(const int index) { + ERR_FAIL_INDEX(index, _entries.size()); + + _entries.remove(index); +} +void FormField::clear_entries() { + _entries.clear(); +} + +int FormField::get_entry_count() const { + return _entries.size(); +} + +Vector FormField::get_entries() { + Vector r; + for (int i = 0; i < _entries.size(); i++) { + r.push_back(_entries[i].get_ref_ptr()); + } + return r; +} +void FormField::set_entries(const Vector &p_arrays) { + _entries.clear(); + for (int i = 0; i < p_arrays.size(); ++i) { + Ref f = Ref(p_arrays[i]); + + _entries.push_back(f); + } +} + Ref FormField::need_to_exist() { Ref f; f.instance(); @@ -434,16 +508,12 @@ Ref FormField::ignore_if_other_field_not_exists(const String &other) return Ref(this); } -void FormField::add_entry(const Ref &field) { - fields.push_back(field); -} - PoolStringArray FormField::validate(const Ref &request) { return call("_validate", request); } PoolStringArray FormField::_validate(Ref request) { - String param = request->get_parameter(name); + String param = request->get_parameter(get_field_name()); if (_ignore_if_not_exists && param == "") { return PoolStringArray(); @@ -459,8 +529,12 @@ PoolStringArray FormField::_validate(Ref request) { PoolStringArray arr; - for (int i = 0; i < fields.size(); ++i) { - arr.append_array(fields.write[i]->validate(request, Ref(this), param)); + for (int i = 0; i < _entries.size(); ++i) { + Ref f = _entries[i]; + + if (f.is_valid()) { + arr.append_array(f->validate(request, Ref(this), param)); + } } return arr; @@ -471,11 +545,61 @@ FormField::FormField() { _ignore_if_other_field_not_exists = false; } FormField::~FormField() { - fields.clear(); + _entries.clear(); } //FormValidator +void FormValidator::add_field(const Ref &field) { + _fields.push_back(field); +} + +Ref FormValidator::get_field(const int index) { + ERR_FAIL_INDEX_V(index, _fields.size(), Ref()); + + return _fields[index]; +} + +void FormValidator::remove_field(const int index) { + ERR_FAIL_INDEX(index, _fields.size()); + + _fields.remove(index); +} +void FormValidator::clear_fields() { + _fields.clear(); +} + +Ref FormValidator::new_field(const String &name, const String &human_name) { + Ref f; + f.instance(); + f->set_field_name(name); + f->set_human_name(human_name); + + _fields.push_back(f); + + return f; +} + +int FormValidator::get_field_count() const { + return _fields.size(); +} + +Vector FormValidator::get_fields() { + Vector r; + for (int i = 0; i < _fields.size(); i++) { + r.push_back(_fields[i].get_ref_ptr()); + } + return r; +} +void FormValidator::set_fields(const Vector &p_arrays) { + _fields.clear(); + for (int i = 0; i < p_arrays.size(); ++i) { + Ref f = Ref(p_arrays[i]); + + _fields.push_back(f); + } +} + PoolStringArray FormValidator::validate(const Ref &request) { return call("_validate", request); } @@ -483,31 +607,20 @@ PoolStringArray FormValidator::validate(const Ref &request) { PoolStringArray FormValidator::_validate(Ref request) { PoolStringArray arr; - for (int i = 0; i < fields.size(); ++i) { - arr.append_array(fields.write[i]->validate(request)); + for (int i = 0; i < _fields.size(); ++i) { + Ref f = _fields[i]; + + if (f.is_valid()) { + arr.append_array(f->validate(request)); + } } return arr; } -void FormValidator::add_field(const Ref &field) { - fields.push_back(field); -} - -Ref FormValidator::new_field(const String &name, const String &human_name) { - Ref f; - f.instance(); - f->name = name; - f->human_name = human_name; - - fields.push_back(f); - - return f; -} - FormValidator::FormValidator() { } FormValidator::~FormValidator() { - fields.clear(); + _fields.clear(); } diff --git a/modules/web/html/form_validator.h b/modules/web/html/form_validator.h index f6dffbf02..a70a31b73 100644 --- a/modules/web/html/form_validator.h +++ b/modules/web/html/form_validator.h @@ -179,13 +179,29 @@ class FormField : public Resource { GDCLASS(FormField, Resource); public: - String name; - String human_name; + String get_field_name() const; + void set_field_name(const String &val); - bool _ignore_if_not_exists; + String get_human_name() const; + void set_human_name(const String &val); - bool _ignore_if_other_field_not_exists; - String _ignore_if_other_field_not_exist_field; + bool get_ignore_if_not_exists() const; + void set_ignore_if_not_exists(const bool &val); + + bool get_ignore_if_other_field_not_exists() const; + void set_ignore_if_other_field_not_exists(const bool &val); + + String get_ignore_if_other_field_not_exist_field() const; + void set_ignore_if_other_field_not_exist_field(const String &val); + + void add_entry(const Ref &field); + Ref get_entry(const int index); + void remove_entry(const int index); + void clear_entries(); + int get_entry_count() const; + + Vector get_entries(); + void set_entries(const Vector &p_arrays); Ref need_to_exist(); Ref need_to_be_int(); @@ -202,15 +218,24 @@ public: Ref ignore_if_not_exists(); Ref ignore_if_other_field_not_exists(const String &other); - void add_entry(const Ref &field); - PoolStringArray validate(const Ref &request); virtual PoolStringArray _validate(Ref request); FormField(); ~FormField(); - Vector> fields; +protected: + static void _bind_methods(); + + String _field_name; + String _human_name; + + bool _ignore_if_not_exists; + + bool _ignore_if_other_field_not_exists; + String _ignore_if_other_field_not_exist_field; + + Vector> _entries; }; //FormValidator @@ -219,17 +244,26 @@ class FormValidator : public Resource { GDCLASS(FormValidator, Resource); public: + void add_field(const Ref &field); + Ref get_field(const int index); + void remove_field(const int index); + void clear_fields(); + Ref new_field(const String &name, const String &human_name); + int get_field_count() const; + + Vector get_fields(); + void set_fields(const Vector &p_arrays); + PoolStringArray validate(const Ref &request); virtual PoolStringArray _validate(Ref request); - void add_field(const Ref &field); - Ref new_field(const String &name, const String &human_name); - FormValidator(); ~FormValidator(); protected: - Vector> fields; + static void _bind_methods(); + + Vector> _fields; }; #endif