Form Class Reference
[Form example]

A simple Form. More...

#include <Form.h>

Inherits Wt::WTable.

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

Private Attributes

WContainerWidgetfeedbackMessages_
WLineEditnameEdit_
WLineEditfirstNameEdit_
WComboBoxcountryEdit_
WComboBoxcityEdit_
WDatePickerbirthDateEdit_
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 35 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::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 160 of file Form.C.

00161 {
00162   if (edit->validate() != WValidator::Valid) {
00163     feedbackMessages_->addWidget(new WText(text));
00164     feedbackMessages_->addWidget(new WBreak());
00165     edit->label()->decorationStyle().setForegroundColor(Wt::red);
00166     edit->setStyleClass("Wt-invalid");
00167 
00168     return false;
00169   } else {
00170     edit->label()->decorationStyle().setForegroundColor(WColor());    
00171     edit->setStyleClass("");
00172 
00173     return true;
00174   }
00175 }

void Form::countryChanged (  )  [private, slot]

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

Definition at line 126 of file Form.C.

00127 {
00128   cityEdit_->clear();
00129   cityEdit_->addItem("");
00130   cityEdit_->setCurrentIndex(-1);
00131 
00132   switch (countryEdit_->currentIndex()) {
00133   case 0:
00134     break;
00135   case 1:
00136     cityEdit_->addItem("Antwerp");
00137     cityEdit_->addItem("Brussels");
00138     cityEdit_->addItem("Oekene");
00139     break;
00140   case 2:
00141     cityEdit_->addItem("Amsterdam");
00142     cityEdit_->addItem("Den Haag");
00143     cityEdit_->addItem("Rotterdam");
00144     break;
00145   case 3:
00146     cityEdit_->addItem("London");
00147     cityEdit_->addItem("Bristol");
00148     cityEdit_->addItem("Oxford");
00149     cityEdit_->addItem("Stonehenge");
00150     break;
00151   case 4:
00152     cityEdit_->addItem("Boston");
00153     cityEdit_->addItem("Chicago");
00154     cityEdit_->addItem("Los Angelos");
00155     cityEdit_->addItem("New York");
00156     break;
00157   }    
00158 }

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   birthDateEdit_ = new WDatePicker(elementAt(row, 2));
00089   label = new WLabel(tr("example.birthdate"), elementAt(row, 0));
00090   label->setBuddy(birthDateEdit_->lineEdit());
00091   birthDateEdit_->lineEdit()->setValidator(new DateValidator(date(1900,Jan,1),
00092                                                  day_clock::local_day()));
00093   birthDateEdit_->lineEdit()->validator()->setMandatory(true);
00094 
00095   // Child count
00096   ++row;
00097   childCountEdit_ = new WLineEdit("0", elementAt(row, 2));
00098   label = new WLabel(tr("example.childcount"),
00099                      elementAt(row, 0));
00100   label->setBuddy(childCountEdit_);
00101   childCountEdit_->setValidator(new WIntValidator(0,30));
00102   childCountEdit_->validator()->setMandatory(true);
00103 
00104   ++row;
00105   remarksEdit_ = new WTextArea(elementAt(row, 2));
00106   remarksEdit_->setColumns(40);
00107   remarksEdit_->setRows(5);
00108   label = new WLabel(tr("example.remarks"),
00109                      elementAt(row, 0));
00110   label->setBuddy(remarksEdit_);
00111 
00112   // Submit
00113   ++row;
00114   WPushButton *submit = new WPushButton(tr("submit"),
00115                                         elementAt(row, 0));
00116   submit->clicked().connect(SLOT(this, Form::submit));
00117   submit->setMargin(15, Top);
00118   elementAt(row, 0)->setColumnSpan(3);
00119   elementAt(row, 0)->setContentAlignment(AlignTop | AlignCenter);
00120 
00121   // Set column widths for label and validation icon
00122   elementAt(2, 0)->resize(WLength(30, WLength::FontEx), WLength::Auto);
00123   elementAt(2, 1)->resize(20, WLength::Auto);
00124 }

void Form::submit (  )  [private, slot]

Submit the form.

Definition at line 194 of file Form.C.

00195 {
00196   if (validate()) {
00197     // do something useful with the data...
00198     std::wstring name
00199       = firstNameEdit_->text() + L" " + nameEdit_->text();
00200 
00201     std::wstring remarks
00202       = remarksEdit_->text();
00203 
00204     clear();
00205 
00206     new WText(WString::fromUTF8("<p>Thank you, {1}, "
00207                                 "for all this precious data.</p>").arg(name),
00208               elementAt(0, 0));
00209     
00210     if (!remarks.empty())
00211       new WText("<p>You had some remarks. Splendid !</p>", elementAt(0, 0));
00212 
00213     wApp->quit();
00214   }
00215 }

bool Form::validate (  )  [private]

Validate the form, and return whether succesfull.

Definition at line 177 of file Form.C.

00178 {
00179   feedbackMessages_->clear();
00180   bool valid = true;
00181 
00182   if (!checkValid(nameEdit_, tr("error.name")))
00183     valid = false;
00184   if (!checkValid(countryEdit_, tr("error.country")))
00185     valid = false;
00186   if (!checkValid(birthDateEdit_->lineEdit(), tr("error.birthdate")))
00187     valid = false;
00188   if (!checkValid(childCountEdit_, tr("error.childcount")))
00189     valid = false;
00190 
00191   return valid;
00192 }


Member Data Documentation

Definition at line 62 of file Form.h.

Definition at line 63 of file Form.h.

Definition at line 60 of file Form.h.

Definition at line 59 of file Form.h.

Definition at line 54 of file Form.h.

Definition at line 57 of file Form.h.

Definition at line 56 of file Form.h.

Definition at line 66 of file Form.h.

Definition at line 64 of file Form.h.


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

Generated on Thu May 13 05:16:01 2010 for Wt by doxygen 1.6.3