Coverage Report

Created: 2026-04-12 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wt/src/Wt/WDateValidator.h
Line
Count
Source
1
// This may look like C code, but it's really -*- C++ -*-
2
/*
3
 * Copyright (C) 2008 Emweb bv, Herent, Belgium.
4
 *
5
 * See the LICENSE file for terms of use.
6
 */
7
8
#ifndef WDATE_VALIDATOR_H_
9
#define WDATE_VALIDATOR_H_
10
11
#include <Wt/WDate.h>
12
#include <Wt/WValidator.h>
13
14
namespace Wt {
15
16
/*! \class WDateValidator Wt/WDateValidator.h Wt/WDateValidator.h
17
 *  \brief A validator for date input.
18
 *
19
 * This validator accepts input in the given date format, and
20
 * optionally checks if the date is within a given range.
21
 *
22
 * \if cpp
23
 * The format string used for validating user input are the same as
24
 * those used by WDate::fromString().
25
 *
26
 * Usage example:
27
 * \code
28
 * Wt::WLineEdit *lineEdit = addWidget(std::make_unique<Wt::WLineEdit>());
29
 * auto validator = std::make_shared<Wt::WDateValidator>();
30
 * validator->setFormat("dd-MM-yyyy");
31
 * lineEdit->setValidator(validator);
32
 * lineEdit->setText("01-03-2008");
33
 * \endcode
34
 * \endif
35
 *
36
 * \if java
37
 * The format string used are the ones accepted by java.text.SimpleDateFormat
38
 * \endif
39
 *
40
 * <h3>i18n</h3>
41
 *
42
 * The strings used in the WDateValidator can be translated by overriding
43
 * the default values for the following localization keys:
44
 * - Wt.WDateValidator.DateTooEarly: The date must be after {1}
45
 * - Wt.WDateValidator.DateTooLate: The date must be before {1}
46
 * - Wt.WDateValidator.WrongDateRange: The date must be between {1} and {2}
47
 * - Wt.WDateValidator.WrongFormat: Must be a date in the format '{1}'
48
 */
49
class WT_API WDateValidator : public WValidator
50
{
51
public:
52
  /*! \brief Creates a date validator.
53
   *
54
   * The validator will accept dates using the current locale's format.
55
   *
56
   * \sa WLocale::currentLocale()
57
   */
58
  WDateValidator();
59
60
  /*! \brief Creates a date validator.
61
   *
62
   * The validator will accept dates in the indicated range using the
63
   * current locale's format.
64
   *
65
   * \sa WLocale::currentLocale()
66
   */
67
  WDateValidator(const WDate& bottom, const WDate& top);
68
69
  /*! \brief Creates a date validator.
70
   *
71
   * The validator will accept dates in the date format \p format.
72
   *
73
   * \if cpp
74
   * The syntax for \p format is as in WDate::fromString()
75
   * \endif
76
   */
77
  WDateValidator(const WT_USTRING& format);
78
79
  /*! \brief Creates a date validator.
80
   *
81
   * The validator will accept only dates within the indicated range
82
   * <i>bottom</i> to <i>top</i>, in the date format \p format.
83
   *
84
   * \if cpp
85
   * The syntax for \p format is as in WDate::fromString()
86
   * \endif
87
   */
88
  WDateValidator(const WT_USTRING& format,
89
                 const WDate& bottom,
90
                 const WDate& top);
91
92
  /*! \brief Sets the bottom of the valid date range.
93
   *
94
   * The default is a null date constructed using WDate().
95
   */
96
  void setBottom(const WDate& bottom);
97
98
  /*! \brief Returns the bottom date of the valid range.
99
   */
100
0
  const WDate& bottom() const { return bottom_; }
101
102
  /*! \brief Sets the top of the valid date range.
103
   *
104
   * The default is a null date constructed using WDate().
105
   */
106
  void setTop(const WDate& top);
107
108
  /*! \brief Returns the top date of the valid range.
109
   */
110
0
  const WDate& top() const { return top_; }
111
112
  /*! \brief Sets the date format used to parse date strings.
113
   *
114
   * \sa WDate::fromString()
115
   */
116
  void setFormat(const WT_USTRING& format);
117
118
  /*! \brief Returns the format string used to parse date strings
119
   *
120
   * \sa setFormat()
121
   */
122
0
  virtual WT_USTRING format() const override { return formats_[0]; }
123
124
  /*! \brief Sets the date formats used to parse date strings.
125
   */
126
  void setFormats(const std::vector<WT_USTRING>& formats);
127
128
  /*! \brief Returns the date formats used to parse date strings.
129
   */
130
0
  const std::vector<WT_USTRING>& formats() const { return formats_; }
131
132
  /*! \brief Validates the given input.
133
   *
134
   * The input is considered valid only when it is blank for a
135
   * non-mandatory field, or represents a date in the given format,
136
   * and within the valid range.
137
   */
138
  virtual Result validate(const WT_USTRING& input) const override;
139
140
  /*! \brief Sets the message to display when the input is not a date.
141
   *
142
   * The default message is "The date must be of the format {1}", with
143
   * as first argument the format string.
144
   */
145
  void setInvalidNotADateText(const WString& text);
146
147
  /*! \brief Returns the message displayed when the input is not a date.
148
   *
149
   * \sa setInvalidNotADateText(const WString&)
150
   */
151
  WString invalidNotADateText() const;
152
153
  /*! \brief Sets the message to display when the date is earlier than bottom.
154
   *
155
   * \if cpp
156
   *
157
   * Depending on whether bottom() and top() are
158
   * defined (see WDate::isNull()), the default message is "The date
159
   * must be between {1} and {2}" or "The date must be after {1}".
160
   *
161
   * \elseif java
162
   *
163
   * The default message is "The date must be between {1} and {2}" or
164
   * "The date must be after {1}".
165
   *
166
   * \endif
167
   */
168
  void setInvalidTooEarlyText(const WString& text);
169
170
  /*! \brief Returns the message displayed when date is too early.
171
   *
172
   * \sa setInvalidTooEarlyText(const WString&)
173
   */
174
  WString invalidTooEarlyText() const;
175
176
  /*! \brief Sets the message to display when the date is later than top.
177
   *
178
   * \if cpp
179
   * Depending on whether bottom() and top() are
180
   * \link WDate::isNull() defined\endlink, the default message is
181
   * "The date must be between {1} and {2}" or "The date must be before {2}".
182
   * \elseif java
183
   * Depending on whether bottom() and top() are defined, the default message is
184
   * "The date must be between {1} and {2}" or "The date must be before {2}".
185
   * \endif
186
   */
187
  void setInvalidTooLateText(const WString& text);
188
189
  /*! \brief Returns the message displayed when the date is too late.
190
   *
191
   * \sa setInvalidTooLateText(const WString&)
192
   */
193
  WString invalidTooLateText() const;
194
195
  virtual std::string javaScriptValidate() const override;
196
197
private:
198
  std::vector<WT_USTRING> formats_;
199
  WDate   bottom_, top_;
200
201
  WString tooEarlyText_;
202
  WString tooLateText_;
203
  WString notADateText_;
204
205
  static void loadJavaScript(WApplication *app);
206
};
207
208
}
209
210
#endif // DATE_VALIDATOR_H_