/src/wt/src/Wt/WRegExpValidator.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 "Wt/WApplication.h" |
8 | | #include "Wt/WRegExpValidator.h" |
9 | | #include "Wt/WString.h" |
10 | | #include "Wt/WStringStream.h" |
11 | | |
12 | | #include "WebUtils.h" |
13 | | |
14 | | #ifndef WT_DEBUG_JS |
15 | | #include "js/WRegExpValidator.min.js" |
16 | | #endif |
17 | | |
18 | | namespace { |
19 | | #ifndef WT_TARGET_JAVA |
20 | | static Wt::RegExpFlag MatchCaseInsensitive = Wt::RegExpFlag::MatchCaseInsensitive; |
21 | | #else // WT_TARGET_JAVA |
22 | | static int MatchCaseInsensitive = 1; |
23 | | #endif // WT_TARGET_JAVA |
24 | | } |
25 | | |
26 | | namespace Wt { |
27 | | |
28 | | WRegExpValidator::WRegExpValidator() |
29 | 0 | : pattern_(), |
30 | 0 | regex_(), |
31 | 0 | noMatchText_() |
32 | 0 | { } |
33 | | |
34 | | WRegExpValidator::WRegExpValidator(const WT_USTRING& pattern) |
35 | 0 | : pattern_(pattern), |
36 | 0 | regex_(pattern.toUTF8()), |
37 | 0 | noMatchText_() |
38 | 0 | { } |
39 | | |
40 | | WRegExpValidator::~WRegExpValidator() |
41 | 0 | { } |
42 | | |
43 | | void WRegExpValidator::setRegExp(const WT_USTRING& pattern) |
44 | 0 | { |
45 | 0 | regex_.assign(pattern.toUTF8()); |
46 | 0 | pattern_ = pattern; |
47 | 0 | repaint(); |
48 | 0 | } |
49 | | |
50 | | void WRegExpValidator::setInvalidNoMatchText(const WString& text) |
51 | 0 | { |
52 | 0 | noMatchText_ = text; |
53 | 0 | repaint(); |
54 | 0 | } |
55 | | |
56 | | WString WRegExpValidator::invalidNoMatchText() const |
57 | 0 | { |
58 | 0 | if (!noMatchText_.empty()) |
59 | 0 | return noMatchText_; |
60 | 0 | else |
61 | 0 | return WString::tr("Wt.WRegExpValidator.Invalid"); |
62 | 0 | } |
63 | | |
64 | | WValidator::Result WRegExpValidator::validate(const WT_USTRING& input) const |
65 | 0 | { |
66 | 0 | if (input.empty()) |
67 | 0 | return WValidator::validate(input); |
68 | | |
69 | 0 | if (std::regex_match(input.toUTF8(), regex_)) |
70 | 0 | return Result(ValidationState::Valid); |
71 | 0 | else |
72 | 0 | return Result(ValidationState::Invalid, invalidNoMatchText()); |
73 | 0 | } |
74 | | |
75 | | void WRegExpValidator::loadJavaScript(WApplication *app) |
76 | 0 | { |
77 | 0 | LOAD_JAVASCRIPT(app, "js/WRegExpValidator.js", "WRegExpValidator", wtjs1); |
78 | 0 | } |
79 | | |
80 | | void WRegExpValidator::setFlags(WFlags<RegExpFlag> flags) |
81 | 0 | { |
82 | 0 | if (flags.value() == this->flags().value()) |
83 | 0 | return; |
84 | | |
85 | 0 | if (flags.value() & static_cast<int>(MatchCaseInsensitive)) |
86 | 0 | regex_.assign(pattern_.toUTF8(), std::regex::icase); |
87 | 0 | else |
88 | 0 | regex_.assign(pattern_.toUTF8()); |
89 | |
|
90 | 0 | repaint(); |
91 | 0 | } |
92 | | |
93 | | WFlags<RegExpFlag> WRegExpValidator::flags() const |
94 | 0 | { |
95 | 0 | if (regex_.flags() & std::regex::icase) |
96 | 0 | return MatchCaseInsensitive; |
97 | 0 | else { |
98 | 0 | #ifndef WT_TARGET_JAVA |
99 | 0 | return WFlags<RegExpFlag>(); |
100 | | #else |
101 | | return (int)0; |
102 | | #endif |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | | std::string WRegExpValidator::javaScriptValidate() const |
107 | 0 | { |
108 | 0 | loadJavaScript(WApplication::instance()); |
109 | |
|
110 | 0 | WStringStream js; |
111 | |
|
112 | 0 | js << "new " WT_CLASS ".WRegExpValidator(" |
113 | 0 | << isMandatory() |
114 | 0 | << ','; |
115 | |
|
116 | 0 | js << WWebWidget::jsStringLiteral(pattern_) |
117 | 0 | << ",'"; |
118 | |
|
119 | 0 | if (regex_.flags() & std::regex::icase) |
120 | 0 | js << 'i'; |
121 | |
|
122 | 0 | js << '\''; |
123 | |
|
124 | 0 | js << ',' << WWebWidget::jsStringLiteral(invalidBlankText()) |
125 | 0 | << ',' << WWebWidget::jsStringLiteral(invalidNoMatchText()) |
126 | 0 | << ");"; |
127 | |
|
128 | 0 | return js.str(); |
129 | 0 | } |
130 | | |
131 | | } |