Coverage Report

Created: 2026-11-25 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wt/src/Wt/WDateTimeValidator.h
Line
Count
Source
1
#ifndef WDATETIMEVALIDATOR_H
2
#define WDATETIMEVALIDATOR_H
3
4
#include <Wt/WDateTime.h>
5
#include <Wt/WValidator.h>
6
7
namespace Wt {
8
9
/*! \class WDateTimeValidator Wt/WDateTimeValidator.h Wt/WDateTimeValidator.h
10
 *  \brief A validator for dateTime input.
11
 *
12
 * This validator accepts input in the given dateTime format, and
13
 * optionally checks if the dateTime is within a given range.
14
 *
15
 * \if cpp
16
 * The format string used for validating user input are the same as
17
 * those used by WDateTime::fromString().
18
 * \endif
19
 *
20
 *  \sa WDateTimeEdit
21
 *  \sa WDateTime
22
 */
23
class WT_API WDateTimeValidator : public WValidator
24
{
25
public:
26
  /*! \brief Creates a datetime validator.
27
   *
28
   * The validator will accept datetimes using the current locale's format.
29
   *
30
   */
31
  WDateTimeValidator();
32
33
  /*! \brief Creates a datetime validator.
34
   *
35
   * The validator will accept only datetimes within the indicated range
36
   * <i>bottom</i> to <i>top</i>.
37
   */
38
  WDateTimeValidator(const WDateTime& bottom, const WDateTime& top);
39
40
  /*! \brief Creates a datetime validator.
41
   *
42
   * The validator will accept dates in the date format \p format.
43
   *
44
   * \if cpp
45
   * The syntax for \p format is as in WDateTime::fromString()
46
   * \endif
47
   */
48
  WDateTimeValidator(const WT_USTRING& format);
49
50
  /*! \brief Creates a datetime validator.
51
   *
52
   * The validator will accept only datetimes within the indicated range
53
   * <i>bottom</i> to <i>top</i>, in the datetime format \p format.
54
   *
55
   * \if cpp
56
   * The syntax for \p format is as in WDateTime::fromString()
57
   * \endif
58
   */
59
  WDateTimeValidator(const WT_USTRING& format,
60
     const WDateTime& bottom,
61
     const WDateTime& top);
62
63
  /*! \brief Sets the bottom of the valid datetime range.
64
   *
65
   * The default is an empty datetime constructed using WDateTime().
66
   */
67
  void setBottom(const WDateTime& bottom);
68
69
  /*! \brief Returns the bottom datetime of the valid range.
70
   */
71
0
  const WDateTime& bottom() const { return bottom_; }
72
73
  /*! \brief Sets the top of the valid datetime range.
74
   *
75
   * The default is an empty datetime constructed using WDateTime::WDateTime().
76
   */
77
  void setTop(const WDateTime& top);
78
79
  /*! \brief Returns the top datetime of the valid range.
80
   */
81
0
  const WDateTime& top() const { return top_; }
82
83
  /*! \brief Sets the datetime format used to parse datetime strings.
84
   *
85
   * \sa setFormats(), WDateTime::fromString()
86
   */
87
  void setFormat(const WT_USTRING& format);
88
89
  /*! \brief Returns the format string used to parse datetime strings
90
   *
91
   * If multiple formats are specified, the first format in the list is
92
   * returned.
93
   *
94
   * \sa setFormat()
95
   */
96
0
  WT_USTRING format() const override { return formats_[0]; }
97
98
  /*! \brief Sets the datetime formats used to parse datetime strings.
99
   *
100
   * When multiple formats are given, the first format that correctly
101
   * parses the input is used.
102
   *
103
   * \sa setFormat(), WDateTime::fromString()
104
   */
105
  void setFormats(const std::vector<WT_USTRING>& formats);
106
107
  /*! \brief Returns the datetime formats used to parse datetime strings.
108
   */
109
0
  const std::vector<WT_USTRING>& formats() const { return formats_; }
110
111
  /*! \brief Validates the given input.
112
   *
113
   * The input is considered valid only when it is blank for a
114
   * non-mandatory field, or represents a datetime in the given
115
   * format, and within the valid range.
116
   */
117
  Result validate(const WT_USTRING& input) const override;
118
119
  std::string javaScriptValidate() const override;
120
121
  /*! \brief Sets the message to display when the input is not a datetime.
122
   *
123
   * The default message is "The date and time must match the format {1}",
124
   * with the format string as the first argument.
125
   */
126
  void setInvalidNotADateTimeText(const WString& text);
127
128
  /*! \brief Returns the message displayed when the input is not a datetime.
129
   *
130
   * \sa setInvalidNotADateTimeText(const WString&)
131
   */
132
  WString invalidNotADateTimeText() const;
133
134
  /*! \brief Sets the message to display when the datetime
135
   *   is earlier than bottom().
136
   *
137
   * \if cpp
138
   *
139
   * Depending on whether bottom() and top() are
140
   * defined (see WDateTime::isNull()), the default message is
141
   * "The date and time must be between {1} and {2}" or
142
   * "The date and time must be after {1}".
143
   *
144
   * \elseif java
145
   *
146
   * The default message is "The date and time must be between
147
   * {1} and {2}" or "The date and time must be after {1}".
148
   *
149
   * \endif
150
   */
151
  void setInvalidTooEarlyText(const WString& text);
152
153
  /*! \brief Returns the message displayed when datetime is too early.
154
   *
155
   * \sa setInvalidTooEarlyText(const WString&)
156
   */
157
  WString invalidTooEarlyText() const;
158
159
  /*! \brief Sets the message to display when the datetime is
160
   *  later than top().
161
   *
162
   * \if cpp
163
   *
164
   * Depending on whether bottom() and top() are
165
   * \link WDateTime::isNull() defined\endlink, the default message is
166
   * "The date and time must be between {1} and {2}" or
167
   * "The date and time must be before {2}".
168
   *
169
   * \elseif java
170
   *
171
   * Depending on whether bottom() and top() are defined,
172
   * the default message is. "The date and time must be between
173
   * {1} and {2}" or "The date and time must be before {2}".
174
   *
175
   * \endif
176
   */
177
  void setInvalidTooLateText(const WString& text);
178
179
  /*! \brief Returns the message displayed when the datetime is too late.
180
   *
181
   * \sa setInvalidTooLateText(const WString&)
182
   */
183
  WString invalidTooLateText() const;
184
185
private:
186
    std::vector<WT_USTRING> formats_;
187
    WDateTime bottom_, top_;
188
189
    WString tooEarlyText_;
190
    WString tooLateText_;
191
    WString notADateTimeText_;
192
193
    static void loadJavaScript(WApplication *app);
194
};
195
196
}
197
#endif // WDATETIMEVALIDATOR_H