Menu

[r482]: / framework / trunk / form.cpp  Maximize  Restore  History

Download this file

348 lines (309 with data), 5.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "form.h"
#include "util.h"
#include <boost/format.hpp>
namespace cppcms {
base_form::~base_form()
{
}
void base_form::append(base_form *ptr)
{
elements.push_back(ptr);
}
bool base_form::validate()
{
bool valid=true;
for(list<base_form *>::iterator p=elements.begin(),e=elements.end();p!=e;++p)
{
valid = valid && (*p)->validate();
}
return valid;
}
void base_form::load(cgicc::Cgicc const &cgi)
{
for(list<base_form *>::iterator p=elements.begin(),e=elements.end();p!=e;++p)
{
(*p)->load(cgi);
}
}
string base_form::render(int how)
{
string output;
for(list<base_form *>::iterator p=elements.begin(),e=elements.end();p!=e;++p) {
output+=(*p)->render(how);
}
return output;
}
namespace widgets {
base_widget::base_widget(string _id,string m) : name(_id), msg(m)
{
is_set=false;
is_valid=false;
}
string base_widget::render(int how)
{
string out;
string input=render_input(how);
string error;
switch(how & error_mask) {
case error_only:
return render_error();
case error_with:
error=render_error();
break;
case error_no:
break;
}
string tmp_msg;
if(!id.empty()) {
tmp_msg="<lablel for=\"" + id + "\">" + msg +"</label>";
}
else {
tmp_msg=name;
}
if(error.empty()) {
char const *frm=NULL;
switch(how & as_mask) {
case as_p:
frm = "<p>%1%: %2%</p>\n";
break;
case as_table:
frm = "<tr><th>%1%</th><td>%2%</td></tr>\n";
break;
case as_ul:
frm = "<li>%1%: %2%</li>\n";
break;
}
assert(frm);
out=(boost::format(frm) % tmp_msg % input).str();
}
else {
char const *frm=NULL;
switch(how & as_mask) {
case as_p:
frm = "<p>%3%</p>\n<p>%1%: %2%</p>\n";
break;
case as_table:
frm = "<tr><th>%1%</th><td>%3% %2%</td></tr>\n";
break;
case as_ul:
frm = "<li>%3% %1%: %2%</li>\n";
break;
}
assert(frm);
error="<ul class=\"errorlist\">" + error + "</ul>";
out=(boost::format(frm) % tmp_msg % input % error).str();
}
return out;
}
string base_widget::render_error()
{
if(is_set && !is_valid)
return escape(error_msg);
return "";
}
string text::render_input(int how)
{
string out="<input type=\"" + type +"\" name=\"";
out+=name;
out+="\"";
if(!id.empty()) {
out+=" id=\"";
out+=id;
out+="\"";
}
if(is_set) {
out+=" value=\"";
out+=escape(value);
out+="\"";
}
if(how & as_xhtml)
out+=" />\n";
else
out+=" >\n";
return out;
}
void text::load(cgicc::Cgicc const &cgi)
{
cgicc::const_form_iterator v=cgi.getElement(name);
if(v!=cgi.getElements().end()) {
value=**v;
is_set=true;
}
}
bool text::validate()
{
if(!is_set)
is_valid=false;
else
if(value.size()<low || (high>=0 && value.size()>(unsigned)high)) {
is_valid=false;
}
else
is_valid=true;
return is_valid;
}
void text::set(string const &s)
{
is_set=true;
value=s;
}
string const &text::get()
{
return value;
}
string textarea::render_input(int how)
{
char const *frm=NULL;
string sid;
if(!id.empty())
sid=" id=\"" + id +"\"";
if(!is_set)
frm="<textarea%3% name=\"%1%\" %2%></textarea>\n";
else
frm="<textarea%4% name=\"%1%\" %2%>%3%</textarea>\n";
string format;
if(rows!=-1 && cols!=-1)
format=(boost::format("rows=\"%d\" cols=\"%d\"") % rows % cols).str();
if(is_set)
return (boost::format(frm) % name % format % escape(value) % sid).str();
return (boost::format(frm) % name % format % sid).str();
}
string checkbox::render_input(int how)
{
string out="<input type=\"checkbox\" name=\"";
out+=name;
out+="\" value=\"";
out+=escape(input_value);
out+="\"";
if(value)
out+=" checked=\"checked\"";
if(!id.empty())
out+=" id=\""+id+"\"";
if(how & as_xhtml)
out+=" />";
else
out+=" >";
return out;
}
string checkbox::render_error()
{
return "";
}
void checkbox::load(cgicc::Cgicc const &cgi)
{
cgicc::const_form_iterator p=cgi.getElements().begin(),e=cgi.getElements().end();
while(p!=e) {
if(p->getName()==name && p->getValue()==input_value) {
value=true;
return;
}
++p;
}
value=false;
}
bool regex_field::validate()
{
if(!text::validate())
return false;
return boost::regex_match(value,exp);
}
boost::regex email::exp_email("^[^@]+@[^@]+$");
void select::add(string v,string opt)
{
option o;
o.value=v;
o.option=opt;
select_list.push_back(o);
}
void select::add(int val,string opt)
{
add((boost::format("%d") % val).str(),opt);
}
void select::set(string v)
{
value=v;
}
void select::set(int v)
{
value=(boost::format("%d") % v).str();
}
string select::get()
{
return value;
}
int select::geti()
{
return atoi(value.c_str());
}
string select::render_input(int how)
{
string out="<select name=\"";
out+=name;
out+="\" ";
if(!id.empty())
out+="id=\""+id+"\" ";
out+=">\n";
for(list<option>::iterator p=select_list.begin(),e=select_list.end();p!=e;++p) {
out+="<option value=\""+p->value+"\" ";
if(p->value==value)
out+=" selected=\"selected\" ";
out+=">";
out+=escape(p->option);
out+="</option>\n";
}
out+="</select>\n";
return out;
}
void select::load(cgicc::Cgicc const &cgi)
{
cgicc::const_form_iterator p=cgi.getElement(name);
if(p==cgi.getElements().end()) {
is_set=false;
}
else {
value=**p;
}
}
bool select::validate()
{
if(!is_set) {
is_valid=false;
return false;
}
for(list<option>::iterator p=select_list.begin(),e=select_list.end();p!=e;++p) {
if(value==p->value)
is_valid=true;
return true;
}
is_valid=false;
return false;
}
string hidden::render(int how)
{
string out="<input type=\"hidden\" name=\"";
out+=name;
out+="\" value=\"";
out+=value;
out+="\" ";
if(!id.empty()) {
out+="id=\""+id+"\"";
}
if(how & as_xhtml)
out+=" />\n";
else
out+=" >\n";
return out;
}
bool password::validate()
{
if(!text::validate())
return false;
if(other)
if(other->value!=value) {
is_valid=false;
return false;
}
return true;
}
} // Namespace widgets
} // namespace cppcms
MongoDB Logo MongoDB