#include <Form.h>
Public Member Functions | |
Form (WContainerWidget *parent=0) | |
Instantiate a new form. | |
Private Slots | |
void | countryChanged () |
The user selected a new country: adjust the cities combo box. | |
void | submit () |
Submit the form. | |
Private Member Functions | |
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_ |
WLineEdit * | birthDateEdit_ |
WLineEdit * | childCountEdit_ |
WLineEdit * | weightEdit_ |
WTextArea * | remarksEdit_ |
Shows how a simple form can made, with an emphasis on how to handle validation.
Definition at line 34 of file Form.h.
Form::Form | ( | WContainerWidget * | parent = 0 |
) |
void Form::countryChanged | ( | ) | [private, slot] |
The user selected a new country: adjust the cities combo box.
Definition at line 131 of file Form.C.
00132 { 00133 cityEdit_->clear(); 00134 cityEdit_->addItem(""); 00135 cityEdit_->setCurrentIndex(-1); 00136 00137 switch (countryEdit_->currentIndex()) { 00138 case 0: 00139 break; 00140 case 1: 00141 cityEdit_->addItem("Antwerp"); 00142 cityEdit_->addItem("Brussels"); 00143 cityEdit_->addItem("Oekene"); 00144 break; 00145 case 2: 00146 cityEdit_->addItem("Amsterdam"); 00147 cityEdit_->addItem("Den Haag"); 00148 cityEdit_->addItem("Rotterdam"); 00149 break; 00150 case 3: 00151 cityEdit_->addItem("London"); 00152 cityEdit_->addItem("Bristol"); 00153 cityEdit_->addItem("Oxford"); 00154 cityEdit_->addItem("Stonehenge"); 00155 break; 00156 case 4: 00157 cityEdit_->addItem("Boston"); 00158 cityEdit_->addItem("Chicago"); 00159 cityEdit_->addItem("Los Angelos"); 00160 cityEdit_->addItem("New York"); 00161 break; 00162 } 00163 }
void Form::submit | ( | ) | [private, slot] |
Submit the form.
Definition at line 199 of file Form.C.
00200 { 00201 if (validate()) { 00202 // do something useful with the data... 00203 std::wstring name 00204 = firstNameEdit_->text() + L" " + nameEdit_->text(); 00205 00206 std::wstring remarks 00207 = remarksEdit_->text(); 00208 00209 clear(); 00210 00211 new WText(WString::fromUTF8("<p>Thank you, {1}, " 00212 "for all this precious data.</p>").arg(name), 00213 elementAt(0, 0)); 00214 00215 if (!remarks.empty()) 00216 new WText("<p>You had some remarks. Splendid !</p>", elementAt(0, 0)); 00217 00218 wApp->quit(); 00219 } 00220 }
void Form::createUI | ( | ) | [private] |
Definition at line 26 of file Form.C.
00027 { 00028 WLabel *label; 00029 int row = 0; 00030 00031 // Title 00032 elementAt(row, 0)->setColumnSpan(3); 00033 elementAt(row, 0)->setContentAlignment(AlignTop | AlignCenter); 00034 elementAt(row, 0)->setPadding(10); 00035 WText *title = new WText(tr("example.form"), 00036 elementAt(row, 0)); 00037 title->decorationStyle().font().setSize(WFont::XLarge); 00038 00039 // error messages 00040 ++row; 00041 elementAt(row, 0)->setColumnSpan(3); 00042 feedbackMessages_ = elementAt(row, 0); 00043 feedbackMessages_->setPadding(5); 00044 00045 WCssDecorationStyle& errorStyle = feedbackMessages_->decorationStyle(); 00046 errorStyle.setForegroundColor(Wt::red); 00047 errorStyle.font().setSize(WFont::Smaller); 00048 errorStyle.font().setWeight(WFont::Bold); 00049 errorStyle.font().setStyle(WFont::Italic); 00050 00051 // Name 00052 ++row; 00053 nameEdit_ = new WLineEdit(elementAt(row, 2)); 00054 label = new WLabel(tr("example.name"), elementAt(row, 0)); 00055 label->setBuddy(nameEdit_); 00056 nameEdit_->setValidator(new WValidator(true)); 00057 nameEdit_->enterPressed().connect(SLOT(this, Form::submit)); 00058 00059 // First name 00060 ++row; 00061 firstNameEdit_ = new WLineEdit(elementAt(row, 2)); 00062 label = new WLabel(tr("example.firstname"), elementAt(row,0)); 00063 label->setBuddy(firstNameEdit_); 00064 00065 // Country 00066 ++row; 00067 countryEdit_ = new WComboBox(elementAt(row, 2)); 00068 countryEdit_->addItem(""); 00069 countryEdit_->addItem("Belgium"); 00070 countryEdit_->addItem("Netherlands"); 00071 countryEdit_->addItem("United Kingdom"); 00072 countryEdit_->addItem("United States"); 00073 label = new WLabel(tr("example.country"), elementAt(row, 0)); 00074 label->setBuddy(countryEdit_); 00075 countryEdit_->setValidator(new WValidator(true)); 00076 countryEdit_->changed().connect(SLOT(this, Form::countryChanged)); 00077 00078 // City 00079 ++row; 00080 cityEdit_ = new WComboBox(elementAt(row, 2)); 00081 cityEdit_->addItem(tr("example.choosecountry")); 00082 label = new WLabel(tr("example.city"), elementAt(row, 0)); 00083 label->setBuddy(cityEdit_); 00084 00085 // Birth date 00086 ++row; 00087 00088 00089 birthDateEdit_ = new WLineEdit(elementAt(row, 2)); 00090 label = new WLabel(tr("example.birthdate"), elementAt(row, 0)); 00091 label->setBuddy(birthDateEdit_); 00092 birthDateEdit_->setValidator(new DateValidator(date(1900,Jan,1), 00093 day_clock::local_day())); 00094 birthDateEdit_->validator()->setMandatory(true); 00095 00096 WDatePicker *picker = new WDatePicker(new WText("..."), 00097 birthDateEdit_, true, 00098 elementAt(row, 2)); 00099 00100 // Child count 00101 ++row; 00102 childCountEdit_ = new WLineEdit("0", elementAt(row, 2)); 00103 label = new WLabel(tr("example.childcount"), 00104 elementAt(row, 0)); 00105 label->setBuddy(childCountEdit_); 00106 childCountEdit_->setValidator(new WIntValidator(0,30)); 00107 childCountEdit_->validator()->setMandatory(true); 00108 00109 ++row; 00110 remarksEdit_ = new WTextArea(elementAt(row, 2)); 00111 remarksEdit_->setColumns(40); 00112 remarksEdit_->setRows(5); 00113 label = new WLabel(tr("example.remarks"), 00114 elementAt(row, 0)); 00115 label->setBuddy(remarksEdit_); 00116 00117 // Submit 00118 ++row; 00119 WPushButton *submit = new WPushButton(tr("submit"), 00120 elementAt(row, 0)); 00121 submit->clicked().connect(SLOT(this, Form::submit)); 00122 submit->setMargin(15, Top); 00123 elementAt(row, 0)->setColumnSpan(3); 00124 elementAt(row, 0)->setContentAlignment(AlignTop | AlignCenter); 00125 00126 // Set column widths for label and validation icon 00127 elementAt(2, 0)->resize(WLength(30, WLength::FontEx), WLength::Auto); 00128 elementAt(2, 1)->resize(20, WLength::Auto); 00129 }
void Form::addValidationStatus | ( | int | row, | |
WFormWidget * | field | |||
) | [private] |
Add a validation feedback for a field.
bool Form::validate | ( | ) | [private] |
Validate the form, and return whether succesfull.
Definition at line 182 of file Form.C.
00183 { 00184 feedbackMessages_->clear(); 00185 bool valid = true; 00186 00187 if (!checkValid(nameEdit_, tr("error.name"))) 00188 valid = false; 00189 if (!checkValid(countryEdit_, tr("error.country"))) 00190 valid = false; 00191 if (!checkValid(birthDateEdit_, tr("error.birthdate"))) 00192 valid = false; 00193 if (!checkValid(childCountEdit_, tr("error.childcount"))) 00194 valid = false; 00195 00196 return valid; 00197 }
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 165 of file Form.C.
00166 { 00167 if (edit->validate() != WValidator::Valid) { 00168 feedbackMessages_->addWidget(new WText(text)); 00169 feedbackMessages_->addWidget(new WBreak()); 00170 edit->label()->decorationStyle().setForegroundColor(Wt::red); 00171 edit->setStyleClass("Wt-invalid"); 00172 00173 return false; 00174 } else { 00175 edit->label()->decorationStyle().setForegroundColor(WColor()); 00176 edit->setStyleClass(""); 00177 00178 return true; 00179 } 00180 }
WContainerWidget* Form::feedbackMessages_ [private] |
WLineEdit* Form::nameEdit_ [private] |
WLineEdit* Form::firstNameEdit_ [private] |
WComboBox* Form::countryEdit_ [private] |
WComboBox* Form::cityEdit_ [private] |
WLineEdit* Form::birthDateEdit_ [private] |
WLineEdit* Form::childCountEdit_ [private] |
WLineEdit* Form::weightEdit_ [private] |
WTextArea* Form::remarksEdit_ [private] |