/src/wt/src/Wt/WValidator.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 | | #include "WebUtils.h" |
7 | | |
8 | | #include "Wt/WFormWidget.h" |
9 | | #include "Wt/WValidator.h" |
10 | | #include "Wt/WString.h" |
11 | | |
12 | | namespace Wt { |
13 | | |
14 | | WValidator::Result::Result() |
15 | 0 | : state_(ValidationState::Invalid) |
16 | 0 | { } |
17 | | |
18 | | WValidator::Result::Result(ValidationState state, const WString& message) |
19 | 0 | : state_(state), |
20 | 0 | message_(message) |
21 | 0 | { } |
22 | | |
23 | | WValidator::Result::Result(ValidationState state) |
24 | 0 | : state_(state), |
25 | 0 | message_(WString::Empty) |
26 | 0 | { } |
27 | | |
28 | | WValidator::WValidator(bool mandatory) |
29 | 0 | : mandatory_(mandatory) |
30 | 0 | { } |
31 | | |
32 | | WValidator::~WValidator() |
33 | 0 | { |
34 | 0 | for (int i = formWidgets_.size() - 1; i >= 0; --i) |
35 | 0 | formWidgets_[i]->setValidator(nullptr); |
36 | 0 | } |
37 | | |
38 | | void WValidator::setMandatory(bool mandatory) |
39 | 0 | { |
40 | 0 | if (mandatory_ != mandatory) { |
41 | 0 | mandatory_ = mandatory; |
42 | 0 | repaint(); |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | | void WValidator::setInvalidBlankText(const WString& text) |
47 | 0 | { |
48 | 0 | mandatoryText_ = text; |
49 | 0 | repaint(); |
50 | 0 | } |
51 | | |
52 | | WString WValidator::invalidBlankText() const |
53 | 0 | { |
54 | 0 | if (!mandatoryText_.empty()) |
55 | 0 | return mandatoryText_; |
56 | 0 | else |
57 | 0 | return WString::tr("Wt.WValidator.Invalid"); |
58 | 0 | } |
59 | | |
60 | | WValidator::Result WValidator::validate(const WT_USTRING& input) const |
61 | 0 | { |
62 | 0 | if (isMandatory()) { |
63 | 0 | if (input.empty()) |
64 | 0 | return Result(ValidationState::InvalidEmpty, invalidBlankText()); |
65 | 0 | } |
66 | | |
67 | 0 | return Result(ValidationState::Valid); |
68 | 0 | } |
69 | | |
70 | | WT_USTRING WValidator::format() const |
71 | 0 | { |
72 | 0 | return WT_USTRING(); |
73 | 0 | } |
74 | | |
75 | | std::string WValidator::javaScriptValidate() const |
76 | 0 | { |
77 | 0 | if (mandatory_) { |
78 | 0 | return "new (function() {" |
79 | 0 | "this.validate = function(text) {" |
80 | 0 | """return { valid: text.length != 0, " |
81 | 0 | """message: " + invalidBlankText().jsStringLiteral() + "}" |
82 | 0 | "};" |
83 | 0 | "})();"; |
84 | 0 | } else { |
85 | 0 | return "new (function() {" |
86 | 0 | "this.validate = function(text) {" |
87 | 0 | """return { valid: true }" |
88 | 0 | "};" |
89 | 0 | "})();"; |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | | std::string WValidator::inputFilter() const |
94 | 0 | { |
95 | 0 | return std::string(); |
96 | 0 | } |
97 | | |
98 | | void WValidator::repaint() |
99 | 0 | { |
100 | 0 | for (unsigned i = 0; i < formWidgets_.size(); ++i) |
101 | 0 | formWidgets_[i]->validatorChanged(); |
102 | |
|
103 | 0 | for (unsigned i = 0; i < parentValidators_.size(); ++i) { |
104 | 0 | parentValidators_[i]->repaint(); |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | void WValidator::addFormWidget(WFormWidget *w) |
109 | 0 | { |
110 | 0 | formWidgets_.push_back(w); |
111 | 0 | } |
112 | | |
113 | | void WValidator::removeFormWidget(WFormWidget *w) |
114 | 0 | { |
115 | 0 | Utils::erase(formWidgets_, w); |
116 | 0 | } |
117 | | |
118 | | void WValidator::addParentValidator(WValidator *v) |
119 | 0 | { |
120 | 0 | parentValidators_.push_back(v); |
121 | 0 | } |
122 | | |
123 | | void WValidator::removeParentValidator(WValidator *v) |
124 | 0 | { |
125 | 0 | Utils::erase(parentValidators_, v); |
126 | 0 | } |
127 | | |
128 | | } |