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 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

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

00018   : WTable(parent)
00019 {
00020   createUI();
00021 }


Member Function Documentation

void Form::countryChanged (  )  [private]

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

Definition at line 124 of file Form.C.

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

void Form::submit (  )  [private]

Submit the form.

Definition at line 192 of file Form.C.

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

void Form::createUI (  )  [private]

Definition at line 23 of file Form.C.

00024 {
00025   WLabel *label;
00026   int row = 0;
00027 
00028   // Title
00029   elementAt(row, 0)->setColumnSpan(3);
00030   elementAt(row, 0)->setContentAlignment(AlignTop | AlignCenter);
00031   elementAt(row, 0)->setPadding(10);
00032   WText *title = new WText(tr("example.form"),
00033                            elementAt(row, 0));
00034   title->decorationStyle().font().setSize(WFont::XLarge);
00035 
00036   // error messages
00037   ++row;
00038   elementAt(row, 0)->setColumnSpan(3);
00039   feedbackMessages_ = elementAt(row, 0);
00040   feedbackMessages_->setPadding(5);
00041 
00042   WCssDecorationStyle& errorStyle = feedbackMessages_->decorationStyle();
00043   errorStyle.setForegroundColor(Wt::red);
00044   errorStyle.font().setSize(WFont::Smaller);
00045   errorStyle.font().setWeight(WFont::Bold);
00046   errorStyle.font().setStyle(WFont::Italic);
00047 
00048   // Name
00049   ++row;
00050   nameEdit_ = new WLineEdit(elementAt(row, 2));
00051   label = new WLabel(tr("example.name"), elementAt(row, 0));
00052   label->setBuddy(nameEdit_);
00053   nameEdit_->setValidator(new WValidator(true));
00054   nameEdit_->enterPressed().connect(SLOT(this, Form::submit));
00055 
00056   // First name
00057   ++row;
00058   firstNameEdit_ = new WLineEdit(elementAt(row, 2));
00059   label = new WLabel(tr("example.firstname"), elementAt(row,0));
00060   label->setBuddy(firstNameEdit_);
00061 
00062   // Country
00063   ++row;
00064   countryEdit_ = new WComboBox(elementAt(row, 2));
00065   countryEdit_->addItem("");
00066   countryEdit_->addItem("Belgium");
00067   countryEdit_->addItem("Netherlands");
00068   countryEdit_->addItem("United Kingdom");
00069   countryEdit_->addItem("United States");
00070   label = new WLabel(tr("example.country"), elementAt(row, 0));
00071   label->setBuddy(countryEdit_);
00072   countryEdit_->setValidator(new WValidator(true));
00073   countryEdit_->changed().connect(SLOT(this, Form::countryChanged));
00074 
00075   // City
00076   ++row;
00077   cityEdit_ = new WComboBox(elementAt(row, 2));
00078   cityEdit_->addItem(tr("example.choosecountry"));
00079   label = new WLabel(tr("example.city"), elementAt(row, 0));
00080   label->setBuddy(cityEdit_);
00081 
00082   // Birth date
00083   ++row;
00084 
00085   birthDateEdit_ = new WDatePicker(elementAt(row, 2));
00086   birthDateEdit_->setBottom(WDate(1900, 1, 1));
00087   birthDateEdit_->setTop(WDate::currentDate());
00088   label = new WLabel(tr("example.birthdate"), elementAt(row, 0));
00089   label->setBuddy(birthDateEdit_->lineEdit());
00090   birthDateEdit_->setFormat("dd/MM/yyyy");
00091   birthDateEdit_->lineEdit()->validator()->setMandatory(true);
00092 
00093   // Child count
00094   ++row;
00095   childCountEdit_ = new WLineEdit("0", elementAt(row, 2));
00096   label = new WLabel(tr("example.childcount"),
00097                      elementAt(row, 0));
00098   label->setBuddy(childCountEdit_);
00099   childCountEdit_->setValidator(new WIntValidator(0,30));
00100   childCountEdit_->validator()->setMandatory(true);
00101 
00102   ++row;
00103   remarksEdit_ = new WTextArea(elementAt(row, 2));
00104   remarksEdit_->setColumns(40);
00105   remarksEdit_->setRows(5);
00106   label = new WLabel(tr("example.remarks"),
00107                      elementAt(row, 0));
00108   label->setBuddy(remarksEdit_);
00109 
00110   // Submit
00111   ++row;
00112   WPushButton *submit = new WPushButton(tr("submit"),
00113                                         elementAt(row, 0));
00114   submit->clicked().connect(SLOT(this, Form::submit));
00115   submit->setMargin(15, Top);
00116   elementAt(row, 0)->setColumnSpan(3);
00117   elementAt(row, 0)->setContentAlignment(AlignTop | AlignCenter);
00118 
00119   // Set column widths for label and validation icon
00120   elementAt(2, 0)->resize(WLength(30, WLength::FontEx), WLength::Auto);
00121   elementAt(2, 1)->resize(20, WLength::Auto);
00122 }

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

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

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.

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


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 Thu May 20 18:14:57 2010 for Wt by doxygen 1.5.6