TreeFrogFramework  2.8
tformvalidator.h
Go to the documentation of this file.
1 #pragma once
2 #include <QPair>
3 #include <QRegularExpression>
4 #include <QStringList>
5 #include <QVariant>
6 #include <TGlobal>
7 
8 
10 public:
12  TFormValidator(const TFormValidator &other);
13  virtual ~TFormValidator() { }
14  TFormValidator &operator=(const TFormValidator &other);
15 
16  void setRule(const QString &key, Tf::ValidationRule rule, bool enable = true, const QString &errorMessage = QString());
17  void setRule(const QString &key, Tf::ValidationRule rule, const QString &errorMessage);
18  void setRule(const QString &key, Tf::ValidationRule rule, const char *errorMessage);
19  void setRule(const QString &key, Tf::ValidationRule rule, float val, const QString &errorMessage = QString());
20  void setRule(const QString &key, Tf::ValidationRule rule, double val, const QString &errorMessage = QString());
21  void setRule(const QString &key, Tf::ValidationRule rule, int val, const QString &errorMessage = QString());
22  void setRule(const QString &key, Tf::ValidationRule rule, int64_t val, const QString &errorMessage = QString());
23  void setPatternRule(const QString &key, const QRegularExpression &rx, const QString &errorMessage = QString());
24  QString message(const QString &key, Tf::ValidationRule rule) const;
25 
26  void setDateFormat(const QString &format);
27  QString dateFormat() const;
28  void setTimeFormat(const QString &format);
29  QString timeFormat() const;
30  void setDateTimeFormat(const QString &format);
31  QString dateTimeFormat() const;
32 
33  virtual bool validate(const QVariantMap &map);
34  bool hasValidationError() const;
35  bool isValidationError(const QString &key) const;
36  QStringList validationErrorKeys() const;
37  QString errorMessage(const QString &key) const;
38  QStringList errorMessages() const;
39  QString value(const QString &key, const QString &defaultValue = QString()) const;
40  Tf::ValidationRule errorRule(const QString &key) const;
41  void setValidationError(const QString &key, const QString &errorMessage);
42 
43 protected:
44  class RuleEntry {
45  public:
46  QString key;
47  int rule;
48  QVariant value;
49  QString message;
50 
51  RuleEntry(const QString &key, int rule, bool enable, const QString &errorMessage);
52  RuleEntry(const QString &key, int rule, int64_t val, const QString &errorMessage);
53  RuleEntry(const QString &key, int rule, double val, const QString &errorMessage);
54  RuleEntry(const QString &key, int rule, const QRegularExpression &rx, const QString &errorMessage);
55  };
56 
57  bool containsRule(const QString &key, Tf::ValidationRule rule) const;
58  void removeRule(const QString &key, Tf::ValidationRule rule);
59 
60  QList<RuleEntry> rules;
61  QList<QPair<QString, int>> errors;
62 
63 private:
64  QString dateFmt;
65  QString timeFmt;
66  QString dateTimeFmt;
67  QVariantMap values;
68 };
69 
70 
71 inline void TFormValidator::setDateFormat(const QString &format)
72 {
73  dateFmt = format;
74 }
75 
76 inline void TFormValidator::setTimeFormat(const QString &format)
77 {
78  timeFmt = format;
79 }
80 
81 inline void TFormValidator::setDateTimeFormat(const QString &format)
82 {
83  dateTimeFmt = format;
84 }
85 
The RuleEntry class is for internal use only.
Definition: tformvalidator.h:44
QVariant value
Definition: tformvalidator.h:48
QString key
Definition: tformvalidator.h:46
QString message
Definition: tformvalidator.h:49
int rule
Definition: tformvalidator.h:47
The TFormValidator class provides form validation for a map-table-based dictionary.
Definition: tformvalidator.h:9
void setDateTimeFormat(const QString &format)
Sets the date and time format to format for validation.
Definition: tformvalidator.h:81
QList< RuleEntry > rules
Definition: tformvalidator.h:60
void setTimeFormat(const QString &format)
Sets the time format to format for validation.
Definition: tformvalidator.h:76
void setDateFormat(const QString &format)
Sets the date format to format for validation.
Definition: tformvalidator.h:71
TFormValidator()
Constructor.
Definition: tformvalidator.h:11
virtual ~TFormValidator()
Destructor.
Definition: tformvalidator.h:13
QList< QPair< QString, int > > errors
Definition: tformvalidator.h:61
ValidationRule
Definition: tfnamespace.h:107
#define T_CORE_EXPORT
Definition: tdeclexport.h:28