/src/wt/src/Wt/WIntValidator.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 | | #ifndef WINTVALIDATOR_H_ |
8 | | #define WINTVALIDATOR_H_ |
9 | | |
10 | | #include <limits> |
11 | | |
12 | | #include <Wt/WValidator.h> |
13 | | |
14 | | namespace Wt { |
15 | | |
16 | | /*! \class WIntValidator Wt/WIntValidator.h Wt/WIntValidator.h |
17 | | * \brief A validator that validates integer user input. |
18 | | * |
19 | | * This validator checks whether user input is an integer number in a |
20 | | * pre-defined range. |
21 | | * |
22 | | * \if cpp |
23 | | * Usage example: |
24 | | * \code |
25 | | * Wt::WLineEdit *lineEdit = addWidget(std::make_unique<Wt::WLineEdit>()); |
26 | | * std::shared_ptr<Wt::WIntValidator> validator = |
27 | | * std::make_shared<Wt::WIntValidator>(0, 100); |
28 | | * lineEdit->setValidator(validator); |
29 | | * lineEdit->setText("50"); |
30 | | * \endcode |
31 | | * \endif |
32 | | * |
33 | | * <h3>i18n</h3> |
34 | | * |
35 | | * The strings used in this class can be translated by overriding |
36 | | * the default values for the following localization keys: |
37 | | * - Wt.WIntValidator.NotAnInteger: Must be an integer number |
38 | | * - Wt.WIntValidator.TooSmall: The number must be larger than {1} |
39 | | * - Wt.WIntValidator.BadRange: The number must be in the range {1} to {2} |
40 | | * - Wt.WIntValidator.TooLarge: The number must be smaller than {1} |
41 | | */ |
42 | | class WT_API WIntValidator : public WValidator |
43 | | { |
44 | | public: |
45 | | /*! \brief Creates a new integer validator that accepts any integer. |
46 | | * |
47 | | * The validator will accept numbers using the current locale's format. |
48 | | * |
49 | | * \sa WLocale::currentLocale() |
50 | | */ |
51 | | WIntValidator(); |
52 | | |
53 | | /*! \brief Creates a new integer validator that accepts integer input |
54 | | * within the given range. |
55 | | * |
56 | | * \sa WLocale::currentLocale() |
57 | | */ |
58 | | WIntValidator(int minimum, int maximum); |
59 | | |
60 | | /*! \brief Returns the bottom of the valid integer range. |
61 | | */ |
62 | 0 | int bottom() const { return bottom_; } |
63 | | |
64 | | /*! \brief Sets the bottom of the valid integer range. |
65 | | * |
66 | | * The default value is the minimum integer value. |
67 | | */ |
68 | | void setBottom(int bottom); |
69 | | |
70 | | /*! \brief Returns the top of the valid integer range. |
71 | | */ |
72 | 0 | int top() const { return top_; } |
73 | | |
74 | | /*! \brief Sets the top of the valid integer range. |
75 | | * |
76 | | * The default value is the maximum integer value. |
77 | | */ |
78 | | void setTop(int top); |
79 | | |
80 | | /*! \brief Sets the range of valid integers. |
81 | | */ |
82 | | virtual void setRange(int bottom, int top); |
83 | | |
84 | | /*! \brief Validates the given input. |
85 | | * |
86 | | * The input is considered valid only when it is blank for a non-mandatory |
87 | | * field, or represents an integer within the valid range. |
88 | | */ |
89 | | virtual Result validate(const WT_USTRING& input) const override; |
90 | | |
91 | | /*! \brief Sets the message to display when the input is not a number. |
92 | | * |
93 | | * The default value is "Must be an integer number." |
94 | | */ |
95 | | void setInvalidNotANumberText(const WString& text); |
96 | | |
97 | | /*! \brief Returns the message displayed when the input is not a number. |
98 | | * |
99 | | * \sa setInvalidNotANumberText(const WString&) |
100 | | */ |
101 | | WString invalidNotANumberText() const; |
102 | | |
103 | | /*! \brief Sets the message to display when the number is too small |
104 | | * |
105 | | * Depending on whether bottom() and top() are real bounds, the |
106 | | * default message is "The number must be between {1} and {2}" or |
107 | | * "The number must be larger than {1}". |
108 | | */ |
109 | | void setInvalidTooSmallText(const WString& text); |
110 | | |
111 | | /*! \brief Returns the message displayed when the number is too small. |
112 | | * |
113 | | * \sa setInvalidTooSmallText(const WString&) |
114 | | */ |
115 | | WString invalidTooSmallText() const; |
116 | | |
117 | | /*! \brief Sets the message to display when the number is too large |
118 | | * |
119 | | * Depending on whether bottom() and top() are real bounds, the |
120 | | * default message is "The number must be between {1} and {2}" or |
121 | | * "The number must be smaller than {2}". |
122 | | */ |
123 | | void setInvalidTooLargeText(const WString& text); |
124 | | |
125 | | /*! \brief Returns the message displayed when the number is too large. |
126 | | * |
127 | | * \sa setInvalidTooLargeText(const WString&) |
128 | | */ |
129 | | WString invalidTooLargeText() const; |
130 | | |
131 | | /*! \brief If true the validator will ignore trailing spaces |
132 | | * |
133 | | * \sa ignoreTrailingSpaces() |
134 | | */ |
135 | | void setIgnoreTrailingSpaces(bool b); |
136 | | |
137 | | /*! \brief Indicates whether the validator should ignore the trailing spaces |
138 | | * \sa setIgnoreTrailingSpaces() |
139 | | */ |
140 | 0 | bool ignoreTrailingSpaces() { return ignoreTrailingSpaces_; } |
141 | | |
142 | | virtual std::string javaScriptValidate() const override; |
143 | | |
144 | | virtual std::string inputFilter() const override; |
145 | | |
146 | | private: |
147 | | int bottom_; |
148 | | int top_; |
149 | | |
150 | | bool ignoreTrailingSpaces_; |
151 | | |
152 | | WString tooSmallText_; |
153 | | WString tooLargeText_; |
154 | | WString nanText_; |
155 | | |
156 | | static void loadJavaScript(WApplication *app); |
157 | | }; |
158 | | |
159 | | } |
160 | | |
161 | | #endif // WINTVALIDATOR_H_ |