Form Class Reference
[Form example]

A simple Form. More...

#include <Form.h>

Inheritance diagram for Form:

Inheritance graph
[legend]

List of all members.

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 WMessage &text)
 Validate a single form field.

Private Attributes

WContainerWidgetfeedbackMessages_
WLineEditnameEdit_
WLineEditfirstNameEdit_
WComboBoxcountryEdit_
WComboBoxcityEdit_
WLineEditbirthDateEdit_
WLineEditchildCountEdit_
WLineEditweightEdit_
WTextArearemarksEdit_


Detailed Description

A simple Form.

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

Definition at line 34 of file Form.h.


Constructor & Destructor Documentation

Form::Form ( WContainerWidget parent = 0  ) 

Instantiate a new form.

Definition at line 20 of file Form.C.

00021   : WTable(parent)
00022 {
00023   createUI();
00024 }


Member Function Documentation

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(L"");
00135   cityEdit_->setCurrentIndex(-1);
00136 
00137   switch (countryEdit_->currentIndex()) {
00138   case 0:
00139     break;
00140   case 1:
00141     cityEdit_->addItem(L"Antwerp");
00142     cityEdit_->addItem(L"Brussels");
00143     cityEdit_->addItem(L"Oekene");
00144     break;
00145   case 2:
00146     cityEdit_->addItem(L"Amsterdam");
00147     cityEdit_->addItem(L"Den Haag");
00148     cityEdit_->addItem(L"Rotterdam");
00149     break;
00150   case 3:
00151     cityEdit_->addItem(L"London");
00152     cityEdit_->addItem(L"Bristol");
00153     cityEdit_->addItem(L"Oxford");
00154     cityEdit_->addItem(L"Stonehenge");
00155     break;
00156   case 4:
00157     cityEdit_->addItem(L"Boston");
00158     cityEdit_->addItem(L"Chicago");
00159     cityEdit_->addItem(L"Los Angelos");
00160     cityEdit_->addItem(L"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     // WMessage with arguments is not yet implemented...
00212     new WText(L"<p>Thank you, "
00213               + name
00214               + L", for all this precious data.</p>", elementAt(0, 0));
00215     
00216     if (!remarks.empty())
00217       new WText(L"<p>You had some remarks. Splendid !</p>", elementAt(0, 0));
00218 
00219     wApp->quit();
00220   }
00221 }

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(L"");
00069   countryEdit_->addItem(L"Belgium");
00070   countryEdit_->addItem(L"Netherlands");
00071   countryEdit_->addItem(L"United Kingdom");
00072   countryEdit_->addItem(L"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(L"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 WMessage &  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 }


Member Data Documentation

Definition at line 53 of file Form.h.

Definition at line 55 of file Form.h.

Definition at line 56 of file Form.h.

Definition at line 58 of file Form.h.

Definition at line 59 of file Form.h.

Definition at line 61 of file Form.h.

Definition at line 62 of file Form.h.

Definition at line 63 of file Form.h.

Definition at line 65 of file Form.h.


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

Generated on Mon Mar 9 08:28:56 2009 for Wt by doxygen 1.5.6