Wt examples 3.1.10
|
#include <Form.h>
Public Member Functions | |
Form (WContainerWidget *parent=0) | |
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_ |
WDatePicker * | birthDateEdit_ |
WLineEdit * | childCountEdit_ |
WLineEdit * | weightEdit_ |
WTextArea * | remarksEdit_ |
A simple Form.
Shows how a simple form can made, with an emphasis on how to handle validation.
Form::Form | ( | WContainerWidget * | parent = 0 | ) |
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.
{ if (edit->validate() != WValidator::Valid) { feedbackMessages_->addWidget(new WText(text)); feedbackMessages_->addWidget(new WBreak()); edit->label()->decorationStyle().setForegroundColor(Wt::red); edit->setStyleClass("Wt-invalid"); return false; } else { edit->label()->decorationStyle().setForegroundColor(WColor()); edit->setStyleClass(""); return true; } }
void Form::countryChanged | ( | ) | [private] |
The user selected a new country: adjust the cities combo box.
Definition at line 124 of file Form.C.
{ cityEdit_->clear(); cityEdit_->addItem(""); cityEdit_->setCurrentIndex(-1); switch (countryEdit_->currentIndex()) { case 0: break; case 1: cityEdit_->addItem("Antwerp"); cityEdit_->addItem("Brussels"); cityEdit_->addItem("Oekene"); break; case 2: cityEdit_->addItem("Amsterdam"); cityEdit_->addItem("Den Haag"); cityEdit_->addItem("Rotterdam"); break; case 3: cityEdit_->addItem("London"); cityEdit_->addItem("Bristol"); cityEdit_->addItem("Oxford"); cityEdit_->addItem("Stonehenge"); break; case 4: cityEdit_->addItem("Boston"); cityEdit_->addItem("Chicago"); cityEdit_->addItem("Los Angelos"); cityEdit_->addItem("New York"); break; } }
void Form::createUI | ( | ) | [private] |
Definition at line 23 of file Form.C.
{ WLabel *label; int row = 0; // Title elementAt(row, 0)->setColumnSpan(3); elementAt(row, 0)->setContentAlignment(AlignTop | AlignCenter); elementAt(row, 0)->setPadding(10); WText *title = new WText(tr("example.form"), elementAt(row, 0)); title->decorationStyle().font().setSize(WFont::XLarge); // error messages ++row; elementAt(row, 0)->setColumnSpan(3); feedbackMessages_ = elementAt(row, 0); feedbackMessages_->setPadding(5); WCssDecorationStyle& errorStyle = feedbackMessages_->decorationStyle(); errorStyle.setForegroundColor(Wt::red); errorStyle.font().setSize(WFont::Smaller); errorStyle.font().setWeight(WFont::Bold); errorStyle.font().setStyle(WFont::Italic); // Name ++row; nameEdit_ = new WLineEdit(elementAt(row, 2)); label = new WLabel(tr("example.name"), elementAt(row, 0)); label->setBuddy(nameEdit_); nameEdit_->setValidator(new WValidator(true)); nameEdit_->enterPressed().connect(this, &Form::submit); // First name ++row; firstNameEdit_ = new WLineEdit(elementAt(row, 2)); label = new WLabel(tr("example.firstname"), elementAt(row,0)); label->setBuddy(firstNameEdit_); // Country ++row; countryEdit_ = new WComboBox(elementAt(row, 2)); countryEdit_->addItem(""); countryEdit_->addItem("Belgium"); countryEdit_->addItem("Netherlands"); countryEdit_->addItem("United Kingdom"); countryEdit_->addItem("United States"); label = new WLabel(tr("example.country"), elementAt(row, 0)); label->setBuddy(countryEdit_); countryEdit_->setValidator(new WValidator(true)); countryEdit_->changed().connect(this, &Form::countryChanged); // City ++row; cityEdit_ = new WComboBox(elementAt(row, 2)); cityEdit_->addItem(tr("example.choosecountry")); label = new WLabel(tr("example.city"), elementAt(row, 0)); label->setBuddy(cityEdit_); // Birth date ++row; birthDateEdit_ = new WDatePicker(elementAt(row, 2)); birthDateEdit_->setBottom(WDate(1900, 1, 1)); birthDateEdit_->setTop(WDate::currentDate()); label = new WLabel(tr("example.birthdate"), elementAt(row, 0)); label->setBuddy(birthDateEdit_->lineEdit()); birthDateEdit_->setFormat("dd/MM/yyyy"); birthDateEdit_->lineEdit()->validator()->setMandatory(true); // Child count ++row; childCountEdit_ = new WLineEdit("0", elementAt(row, 2)); label = new WLabel(tr("example.childcount"), elementAt(row, 0)); label->setBuddy(childCountEdit_); childCountEdit_->setValidator(new WIntValidator(0,30)); childCountEdit_->validator()->setMandatory(true); ++row; remarksEdit_ = new WTextArea(elementAt(row, 2)); remarksEdit_->setColumns(40); remarksEdit_->setRows(5); label = new WLabel(tr("example.remarks"), elementAt(row, 0)); label->setBuddy(remarksEdit_); // Submit ++row; WPushButton *submit = new WPushButton(tr("submit"), elementAt(row, 0)); submit->clicked().connect(this, &Form::submit); submit->setMargin(15, Top); elementAt(row, 0)->setColumnSpan(3); elementAt(row, 0)->setContentAlignment(AlignTop | AlignCenter); // Set column widths for label and validation icon elementAt(2, 0)->resize(WLength(30, WLength::FontEx), WLength::Auto); elementAt(2, 1)->resize(20, WLength::Auto); }
void Form::submit | ( | ) | [private] |
Submit the form.
Definition at line 192 of file Form.C.
{ if (validate()) { // do something useful with the data... std::wstring name = firstNameEdit_->text() + L" " + nameEdit_->text(); std::wstring remarks = remarksEdit_->text(); clear(); new WText(WString::fromUTF8("<p>Thank you, {1}, " "for all this precious data.</p>").arg(name), elementAt(0, 0)); if (!remarks.empty()) new WText("<p>You had some remarks. Splendid !</p>", elementAt(0, 0)); wApp->quit(); } }
bool Form::validate | ( | ) | [private] |
Validate the form, and return whether succesfull.
Definition at line 175 of file Form.C.
{ feedbackMessages_->clear(); bool valid = true; if (!checkValid(nameEdit_, tr("error.name"))) valid = false; if (!checkValid(countryEdit_, tr("error.country"))) valid = false; if (!checkValid(birthDateEdit_->lineEdit(), tr("error.birthdate"))) valid = false; if (!checkValid(childCountEdit_, tr("error.childcount"))) valid = false; return valid; }
WDatePicker* Form::birthDateEdit_ [private] |
WLineEdit* Form::childCountEdit_ [private] |
WComboBox* Form::cityEdit_ [private] |
WComboBox* Form::countryEdit_ [private] |
WContainerWidget* Form::feedbackMessages_ [private] |
WLineEdit* Form::firstNameEdit_ [private] |
WLineEdit* Form::nameEdit_ [private] |
WTextArea* Form::remarksEdit_ [private] |
WLineEdit* Form::weightEdit_ [private] |