/src/wt/src/Wt/WIntValidator.C
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2008 Emweb bv, Herent, Belgium. |
3 | | * |
4 | | * See the LICENSE file for terms of use. |
5 | | */ |
6 | | |
7 | | #include <boost/algorithm/string.hpp> |
8 | | |
9 | | #include "Wt/WApplication.h" |
10 | | #include "Wt/WIntValidator.h" |
11 | | #include "Wt/WString.h" |
12 | | #include "Wt/WStringStream.h" |
13 | | |
14 | | |
15 | | #ifndef WT_DEBUG_JS |
16 | | #include "js/WIntValidator.min.js" |
17 | | #endif |
18 | | |
19 | | namespace Wt { |
20 | | |
21 | | WIntValidator::WIntValidator() |
22 | 0 | : bottom_(std::numeric_limits<int>::min()), |
23 | 0 | top_(std::numeric_limits<int>::max()), |
24 | 0 | ignoreTrailingSpaces_(false) |
25 | 0 | { } |
26 | | |
27 | | WIntValidator::WIntValidator(int bottom, int top) |
28 | 0 | : bottom_(bottom), |
29 | 0 | top_(top), |
30 | 0 | ignoreTrailingSpaces_(false) |
31 | 0 | { } |
32 | | |
33 | | void WIntValidator::setBottom(int bottom) |
34 | 0 | { |
35 | 0 | if (bottom != bottom_) { |
36 | 0 | bottom_ = bottom; |
37 | 0 | repaint(); |
38 | 0 | } |
39 | 0 | } |
40 | | |
41 | | void WIntValidator::setTop(int top) |
42 | 0 | { |
43 | 0 | if (top != top_) { |
44 | 0 | top_ = top; |
45 | 0 | repaint(); |
46 | 0 | } |
47 | 0 | } |
48 | | |
49 | | void WIntValidator::setRange(int bottom, int top) |
50 | 0 | { |
51 | 0 | setBottom(bottom); |
52 | 0 | setTop(top); |
53 | 0 | } |
54 | | |
55 | | void WIntValidator::setInvalidNotANumberText(const WString& text) |
56 | 0 | { |
57 | 0 | nanText_ = text; |
58 | 0 | repaint(); |
59 | 0 | } |
60 | | |
61 | | WString WIntValidator::invalidNotANumberText() const |
62 | 0 | { |
63 | 0 | if (!nanText_.empty()) |
64 | 0 | return nanText_; |
65 | 0 | else |
66 | 0 | return WString::tr("Wt.WIntValidator.NotAnInteger"); |
67 | 0 | } |
68 | | |
69 | | void WIntValidator::setInvalidTooSmallText(const WString& text) |
70 | 0 | { |
71 | 0 | tooSmallText_ = text; |
72 | 0 | repaint(); |
73 | 0 | } |
74 | | |
75 | | WString WIntValidator::invalidTooSmallText() const |
76 | 0 | { |
77 | 0 | if (!tooSmallText_.empty()) { |
78 | 0 | return WString(tooSmallText_).arg(bottom_).arg(top_); |
79 | 0 | } else |
80 | 0 | if (bottom_ == std::numeric_limits<int>::min()) |
81 | 0 | return WString(); |
82 | 0 | else |
83 | 0 | if (top_ == std::numeric_limits<int>::max()) |
84 | 0 | return WString::tr("Wt.WIntValidator.TooSmall").arg(bottom_); |
85 | 0 | else |
86 | 0 | return WString::tr("Wt.WIntValidator.BadRange").arg(bottom_).arg(top_); |
87 | 0 | } |
88 | | |
89 | | void WIntValidator::setInvalidTooLargeText(const WString& text) |
90 | 0 | { |
91 | 0 | tooLargeText_ = text; |
92 | 0 | repaint(); |
93 | 0 | } |
94 | | |
95 | | WString WIntValidator::invalidTooLargeText() const |
96 | 0 | { |
97 | 0 | if (!tooLargeText_.empty()) { |
98 | 0 | return WString(tooLargeText_).arg(bottom_).arg(top_); |
99 | 0 | } else |
100 | 0 | if (top_ == std::numeric_limits<int>::max()) |
101 | 0 | return WString(); |
102 | 0 | else |
103 | 0 | if (bottom_ == std::numeric_limits<int>::min()) |
104 | 0 | return WString::tr("Wt.WIntValidator.TooLarge").arg(top_); |
105 | 0 | else |
106 | 0 | return WString::tr("Wt.WIntValidator.BadRange").arg(bottom_).arg(top_); |
107 | 0 | } |
108 | | |
109 | 0 | void WIntValidator::setIgnoreTrailingSpaces(bool b) { |
110 | 0 | if(ignoreTrailingSpaces_ != b) { |
111 | 0 | ignoreTrailingSpaces_ = b; |
112 | 0 | repaint(); |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | |
117 | | WValidator::Result WIntValidator::validate(const WT_USTRING& input) const |
118 | 0 | { |
119 | 0 | if (input.empty()) |
120 | 0 | return WValidator::validate(input); |
121 | | |
122 | 0 | std::string text = input.toUTF8(); |
123 | |
|
124 | 0 | if (ignoreTrailingSpaces_) |
125 | 0 | boost::trim(text); |
126 | |
|
127 | 0 | try { |
128 | 0 | int i = WLocale::currentLocale().toInt(text); |
129 | |
|
130 | 0 | if (i < bottom_) |
131 | 0 | return Result(ValidationState::Invalid, invalidTooSmallText()); |
132 | 0 | else if (i > top_) |
133 | 0 | return Result(ValidationState::Invalid, invalidTooLargeText()); |
134 | 0 | else |
135 | 0 | return Result(ValidationState::Valid); |
136 | 0 | } catch (std::exception& e) { |
137 | 0 | return Result(ValidationState::Invalid, invalidNotANumberText()); |
138 | 0 | } |
139 | 0 | } |
140 | | |
141 | | void WIntValidator::loadJavaScript(WApplication *app) |
142 | 0 | { |
143 | 0 | LOAD_JAVASCRIPT(app, "js/WIntValidator.js", "WIntValidator", wtjs1); |
144 | 0 | } |
145 | | |
146 | | std::string WIntValidator::javaScriptValidate() const |
147 | 0 | { |
148 | 0 | loadJavaScript(WApplication::instance()); |
149 | |
|
150 | 0 | WStringStream js; |
151 | |
|
152 | 0 | js << "new " WT_CLASS ".WIntValidator(" |
153 | 0 | << isMandatory() |
154 | 0 | << ','; |
155 | |
|
156 | 0 | if (bottom_ != std::numeric_limits<int>::min()) |
157 | 0 | js << bottom_; |
158 | 0 | else |
159 | 0 | js << "null"; |
160 | |
|
161 | 0 | js << ','; |
162 | |
|
163 | 0 | if (top_ != std::numeric_limits<int>::max()) |
164 | 0 | js << top_; |
165 | 0 | else |
166 | 0 | js << "null"; |
167 | |
|
168 | 0 | js << "," << WWebWidget::jsStringLiteral(WLocale::currentLocale() |
169 | 0 | .groupSeparator()) |
170 | 0 | << ',' << invalidBlankText().jsStringLiteral() |
171 | 0 | << ',' << invalidNotANumberText().jsStringLiteral() |
172 | 0 | << ',' << invalidTooSmallText().jsStringLiteral() |
173 | 0 | << ',' << invalidTooLargeText().jsStringLiteral() |
174 | 0 | << ");"; |
175 | |
|
176 | 0 | return js.str(); |
177 | 0 | } |
178 | | |
179 | | std::string WIntValidator::inputFilter() const |
180 | 0 | { |
181 | 0 | return "[-+0-9]"; |
182 | 0 | } |
183 | | |
184 | | } |