Wt examples  3.3.0
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Form Class Reference

A simple Form. More...

#include <Form.h>

Inheritance diagram for Form:
Inheritance graph
[legend]

Public Member Functions

 Form (WContainerWidget *parent=0)
 Instantiate a new form. More...
 
- Public Member Functions inherited from Wt::WTable
 WTable (WContainerWidget *parent=0)
 
 ~WTable ()
 
WTableCellelementAt (int row, int column)
 
WTableRowrowAt (int row)
 
WTableColumncolumnAt (int column)
 
void removeCell (WTableCell *item)
 
void removeCell (int row, int column)
 
WTableRowinsertRow (int row)
 
void deleteRow (int row)
 
WTableColumninsertColumn (int column)
 
void deleteColumn (int column)
 
void clear ()
 
int rowCount () const
 
int columnCount () const
 
void setHeaderCount (int count, Orientation orientation=Horizontal)
 
int headerCount (Orientation orientation=Horizontal)
 
void moveRow (int from, int to)
 
void moveColumn (int from, int to)
 

Private Member Functions

void countryChanged ()
 The user selected a new country: adjust the cities combo box. More...
 
void submit ()
 Submit the form. More...
 
void createUI ()
 
void addValidationStatus (int row, WFormWidget *field)
 Add a validation feedback for a field. More...
 
bool validate ()
 Validate the form, and return whether succesfull. More...
 
bool checkValid (WFormWidget *edit, const WString &text)
 Validate a single form field. More...
 

Private Attributes

WContainerWidgetfeedbackMessages_
 
WLineEditnameEdit_
 
WLineEditfirstNameEdit_
 
WComboBoxcountryEdit_
 
WComboBoxcityEdit_
 
WDateEditbirthDateEdit_
 
WLineEditchildCountEdit_
 
WLineEditweightEdit_
 
WTextArearemarksEdit_
 

Additional Inherited Members

- Protected Member Functions inherited from Wt::WWidget
virtual void enableAjax ()=0
 
virtual void propagateSetEnabled (bool enabled)=0
 
virtual void render (WFlags< RenderFlag > flags)
 

Detailed Description

A simple Form.

Shows how a simple form can made, with an emphasis on how to handle validation.

Definition at line 35 of file Form.h.

Constructor & Destructor Documentation

Form::Form ( WContainerWidget parent = 0)

Instantiate a new form.

Definition at line 17 of file Form.C.

18  : WTable(parent)
19 {
20  createUI();
21 }

Member Function Documentation

void Form::addValidationStatus ( int  row,
WFormWidget field 
)
private

Add a validation feedback for a field.

bool Form::checkValid ( WFormWidget edit,
const WString text 
)
private

Validate a single form field.

Checks the given field, and appends the given text to the error messages on problems.

Definition at line 158 of file Form.C.

159 {
160  if (edit->validate() != WValidator::Valid) {
161  feedbackMessages_->addWidget(new WText(text));
162  feedbackMessages_->addWidget(new WBreak());
163  edit->label()->decorationStyle().setForegroundColor(Wt::red);
164  edit->setStyleClass("Wt-invalid");
165 
166  return false;
167  } else {
168  edit->label()->decorationStyle().setForegroundColor(WColor());
169  edit->setStyleClass("");
170 
171  return true;
172  }
173 }
void Form::countryChanged ( )
private

The user selected a new country: adjust the cities combo box.

Definition at line 124 of file Form.C.

125 {
126  cityEdit_->clear();
127  cityEdit_->addItem("");
129 
130  switch (countryEdit_->currentIndex()) {
131  case 0:
132  break;
133  case 1:
134  cityEdit_->addItem("Antwerp");
135  cityEdit_->addItem("Brussels");
136  cityEdit_->addItem("Oekene");
137  break;
138  case 2:
139  cityEdit_->addItem("Amsterdam");
140  cityEdit_->addItem("Den Haag");
141  cityEdit_->addItem("Rotterdam");
142  break;
143  case 3:
144  cityEdit_->addItem("London");
145  cityEdit_->addItem("Bristol");
146  cityEdit_->addItem("Oxford");
147  cityEdit_->addItem("Stonehenge");
148  break;
149  case 4:
150  cityEdit_->addItem("Boston");
151  cityEdit_->addItem("Chicago");
152  cityEdit_->addItem("Los Angelos");
153  cityEdit_->addItem("New York");
154  break;
155  }
156 }
void Form::createUI ( )
private

Definition at line 23 of file Form.C.

24 {
25  WLabel *label;
26  int row = 0;
27 
28  // Title
29  elementAt(row, 0)->setColumnSpan(3);
30  elementAt(row, 0)->setContentAlignment(AlignTop | AlignCenter);
31  elementAt(row, 0)->setPadding(10);
32  WText *title = new WText(tr("example.form"),
33  elementAt(row, 0));
34  title->decorationStyle().font().setSize(WFont::XLarge);
35 
36  // error messages
37  ++row;
38  elementAt(row, 0)->setColumnSpan(3);
39  feedbackMessages_ = elementAt(row, 0);
40  feedbackMessages_->setPadding(5);
41 
43  errorStyle.setForegroundColor(Wt::red);
44  errorStyle.font().setSize(WFont::Smaller);
45  errorStyle.font().setWeight(WFont::Bold);
46  errorStyle.font().setStyle(WFont::Italic);
47 
48  // Name
49  ++row;
50  nameEdit_ = new WLineEdit(elementAt(row, 2));
51  label = new WLabel(tr("example.name"), elementAt(row, 0));
52  label->setBuddy(nameEdit_);
53  nameEdit_->setValidator(new WValidator(true));
54  nameEdit_->enterPressed().connect(this, &Form::submit);
55 
56  // First name
57  ++row;
58  firstNameEdit_ = new WLineEdit(elementAt(row, 2));
59  label = new WLabel(tr("example.firstname"), elementAt(row,0));
60  label->setBuddy(firstNameEdit_);
61 
62  // Country
63  ++row;
64  countryEdit_ = new WComboBox(elementAt(row, 2));
65  countryEdit_->addItem("");
66  countryEdit_->addItem("Belgium");
67  countryEdit_->addItem("Netherlands");
68  countryEdit_->addItem("United Kingdom");
69  countryEdit_->addItem("United States");
70  label = new WLabel(tr("example.country"), elementAt(row, 0));
71  label->setBuddy(countryEdit_);
72  countryEdit_->setValidator(new WValidator(true));
73  countryEdit_->changed().connect(this, &Form::countryChanged);
74 
75  // City
76  ++row;
77  cityEdit_ = new WComboBox(elementAt(row, 2));
78  cityEdit_->addItem(tr("example.choosecountry"));
79  label = new WLabel(tr("example.city"), elementAt(row, 0));
80  label->setBuddy(cityEdit_);
81 
82  // Birth date
83  ++row;
84 
85  birthDateEdit_ = new WDateEdit(elementAt(row, 2));
86  birthDateEdit_->setBottom(WDate(1900, 1, 1));
87  birthDateEdit_->setTop(WDate::currentDate());
88  label = new WLabel(tr("example.birthdate"), elementAt(row, 0));
89  label->setBuddy(birthDateEdit_);
90  birthDateEdit_->setFormat("dd/MM/yyyy");
92 
93  // Child count
94  ++row;
95  childCountEdit_ = new WLineEdit("0", elementAt(row, 2));
96  label = new WLabel(tr("example.childcount"),
97  elementAt(row, 0));
98  label->setBuddy(childCountEdit_);
99  childCountEdit_->setValidator(new WIntValidator(0,30));
100  childCountEdit_->validator()->setMandatory(true);
101 
102  ++row;
103  remarksEdit_ = new WTextArea(elementAt(row, 2));
105  remarksEdit_->setRows(5);
106  label = new WLabel(tr("example.remarks"),
107  elementAt(row, 0));
108  label->setBuddy(remarksEdit_);
109 
110  // Submit
111  ++row;
112  WPushButton *submit = new WPushButton(tr("submit"),
113  elementAt(row, 0));
114  submit->clicked().connect(this, &Form::submit);
115  submit->setMargin(15, Top);
116  elementAt(row, 0)->setColumnSpan(3);
117  elementAt(row, 0)->setContentAlignment(AlignTop | AlignCenter);
118 
119  // Set column widths for label and validation icon
120  elementAt(2, 0)->resize(WLength(30, WLength::FontEx), WLength::Auto);
121  elementAt(2, 1)->resize(20, WLength::Auto);
122 }
void Form::submit ( )
private

Submit the form.

Definition at line 192 of file Form.C.

193 {
194  if (validate()) {
195  // do something useful with the data...
196  std::wstring name
197  = firstNameEdit_->text() + L" " + nameEdit_->text();
198 
199  std::wstring remarks
200  = remarksEdit_->text();
201 
202  clear();
203 
204  new WText(WString::fromUTF8("<p>Thank you, {1}, "
205  "for all this precious data.</p>").arg(name),
206  elementAt(0, 0));
207 
208  if (!remarks.empty())
209  new WText("<p>You had some remarks. Splendid !</p>", elementAt(0, 0));
210 
211  wApp->quit();
212  }
213 }
bool Form::validate ( )
private

Validate the form, and return whether succesfull.

Definition at line 175 of file Form.C.

176 {
177  feedbackMessages_->clear();
178  bool valid = true;
179 
180  if (!checkValid(nameEdit_, tr("error.name")))
181  valid = false;
182  if (!checkValid(countryEdit_, tr("error.country")))
183  valid = false;
184  if (!checkValid(birthDateEdit_, tr("error.birthdate")))
185  valid = false;
186  if (!checkValid(childCountEdit_, tr("error.childcount")))
187  valid = false;
188 
189  return valid;
190 }

Member Data Documentation

WDateEdit* Form::birthDateEdit_
private

Definition at line 61 of file Form.h.

WLineEdit* Form::childCountEdit_
private

Definition at line 62 of file Form.h.

WComboBox* Form::cityEdit_
private

Definition at line 59 of file Form.h.

WComboBox* Form::countryEdit_
private

Definition at line 58 of file Form.h.

WContainerWidget* Form::feedbackMessages_
private

Definition at line 53 of file Form.h.

WLineEdit* Form::firstNameEdit_
private

Definition at line 56 of file Form.h.

WLineEdit* Form::nameEdit_
private

Definition at line 55 of file Form.h.

WTextArea* Form::remarksEdit_
private

Definition at line 65 of file Form.h.

WLineEdit* Form::weightEdit_
private

Definition at line 63 of file Form.h.


The documentation for this class was generated from the following files:

Generated on Fri May 31 2013 for the C++ Web Toolkit (Wt) by doxygen 1.8.3.1