Wt examples: Form Class Reference

Wt examples 4.10.4

Loading...
Searching...
No Matches
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:
[legend]

## Public Member Functions
| Form ()
| Instantiate a new form.

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

## Private Attributes
WContainerWidget * | feedbackMessages_
WLineEdit * | nameEdit_
WLineEdit * | firstNameEdit_
WComboBox * | countryEdit_
WComboBox * | cityEdit_
WDateEdit * | birthDateEdit_
WLineEdit * | childCountEdit_
WLineEdit * | weightEdit_
WTextArea * | remarksEdit_

## 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::Form | ( |  | ) |

Instantiate a new form.

Definition at line 17 of file Form.C.

18 : WTable()
19{
20 createUI();
21}
Form::createUI
void createUI()
Definition Form.C:23

## Member Function Documentation

## ◆ addValidationStatus()

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

Add a validation feedback for a field.

## ◆ checkValid()

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 153 of file Form.C.

154{
155 if (edit->validate() != ValidationState::Valid) {
156 feedbackMessages_->addWidget(std::make_unique<WText>(text));
157 feedbackMessages_->addWidget(std::make_unique<WBreak>());
158 edit->label()->decorationStyle().setForegroundColor(WColor("red"));
159 edit->setStyleClass("Wt-invalid");
160 return false;
161 } else {
162 edit->label()->decorationStyle().setForegroundColor(WColor());
163 edit->setStyleClass("");
164
165 return true;
166 }
167}
Form::feedbackMessages_
WContainerWidget * feedbackMessages_
Definition Form.h:53

## ◆ countryChanged()

void Form::countryChanged | ( |  | ) |
| private

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

Definition at line 119 of file Form.C.

120{
121 cityEdit_->clear();
122 cityEdit_->addItem("");
123 cityEdit_->setCurrentIndex(-1);
124
125 switch (countryEdit_->currentIndex()) {
126 case 0:
127 break;
128 case 1:
129 cityEdit_->addItem("Antwerp");
130 cityEdit_->addItem("Brussels");
131 cityEdit_->addItem("Oekene");
132 break;
133 case 2:
134 cityEdit_->addItem("Amsterdam");
135 cityEdit_->addItem("Den Haag");
136 cityEdit_->addItem("Rotterdam");
137 break;
138 case 3:
139 cityEdit_->addItem("London");
140 cityEdit_->addItem("Bristol");
141 cityEdit_->addItem("Oxford");
142 cityEdit_->addItem("Stonehenge");
143 break;
144 case 4:
145 cityEdit_->addItem("Boston");
146 cityEdit_->addItem("Chicago");
147 cityEdit_->addItem("Los Angeles");
148 cityEdit_->addItem("New York");
149 break;
150 }
151}
Form::cityEdit_
WComboBox * cityEdit_
Definition Form.h:59
Form::countryEdit_
WComboBox * countryEdit_
Definition Form.h:58

## ◆ createUI()

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

## ◆ submit()

void Form::submit | ( |  | ) |
| private

Submit the form.

Definition at line 186 of file Form.C.

187{
188 if (validate()) {
189 // do something useful with the data...
190 WString name = WString("{1} {2}")
191 .arg(firstNameEdit_->text())
192 .arg(nameEdit_->text());
193
194 WString remarks
195 = remarksEdit_->text();
196
197 clear();
198
199 elementAt(0,0)->addWidget(std::make_unique<WText>(
200 WString("<p>Thank you, {1}, "
201 "for all this precious data.</p>").arg(name)));
202
203 if (!remarks.empty())
204 elementAt(0,0)->addWidget(std::make_unique<WText>(
205 WString("<p>You had some remarks. Splendid !</p>")));
206
207 wApp->quit();
208 }
209}
Form::validate
bool validate()
Validate the form, and return whether succesfull.
Definition Form.C:169

## ◆ validate()

bool Form::validate | ( |  | ) |
| private

Validate the form, and return whether succesfull.

Definition at line 169 of file Form.C.

170{
171 feedbackMessages_->clear();
172 bool valid = true;
173
174 if (!checkValid(nameEdit_, tr("error.name")))
175 valid = false;
176 if (!checkValid(countryEdit_, tr("error.country")))
177 valid = false;
178 if (!checkValid(birthDateEdit_, tr("error.birthdate")))
179 valid = false;
180 if (!checkValid(childCountEdit_, tr("error.childcount")))
181 valid = false;
182
183 return valid;
184}
Form::checkValid
bool checkValid(WFormWidget *edit, const WString &text)
Validate a single form field.
Definition Form.C:153

## Member Data Documentation

## ◆ birthDateEdit_

WDateEdit* Form::birthDateEdit_
| private

Definition at line 61 of file Form.h.

## ◆ childCountEdit_

WLineEdit* Form::childCountEdit_
| private

Definition at line 62 of file Form.h.

## ◆ cityEdit_

WComboBox* Form::cityEdit_
| private

Definition at line 59 of file Form.h.

## ◆ countryEdit_

WComboBox* Form::countryEdit_
| private

Definition at line 58 of file Form.h.

## ◆ feedbackMessages_

WContainerWidget* Form::feedbackMessages_
| private

Definition at line 53 of file Form.h.

## ◆ firstNameEdit_

WLineEdit* Form::firstNameEdit_
| private

Definition at line 56 of file Form.h.

## ◆ nameEdit_

WLineEdit* Form::nameEdit_
| private

Definition at line 55 of file Form.h.

## ◆ remarksEdit_

WTextArea* Form::remarksEdit_
| private

Definition at line 65 of file Form.h.

## ◆ weightEdit_

WLineEdit* Form::weightEdit_
| private

Definition at line 63 of file Form.h.

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

- form/Form.h
- form/Form.C