Wt
3.3.8
|
A basic model class for forms. More...
#include <Wt/WFormModel>
Public Types | |
typedef const char * | Field |
A type to identify a field. More... | |
Public Types inherited from Wt::WObject | |
typedef void(WObject::* | Method) () |
Typedef for a WObject method without arguments. | |
Public Member Functions | |
WFormModel (WObject *parent=0) | |
Constructor. More... | |
void | addField (Field field, const WString &info=WString::Empty) |
Adds a field. More... | |
void | removeField (Field field) |
Removes a field. More... | |
std::vector< Field > | fields () const |
Returns the fields. More... | |
virtual void | reset () |
Resets the model. More... | |
virtual bool | validate () |
Validates the current input. More... | |
bool | valid () const |
Returns the current overall validation state. More... | |
void | setVisible (Field field, bool visible) |
Sets whether a field is visible. More... | |
virtual bool | isVisible (Field field) const |
Returns whether a field is visible. More... | |
void | setReadOnly (Field field, bool readOnly) |
Sets whether a field is read-only. More... | |
virtual bool | isReadOnly (Field field) const |
Returns whether a field is read only. More... | |
virtual WString | label (Field field) const |
Returns a field label. More... | |
virtual void | setValue (Field field, const boost::any &value) |
Sets the field value. More... | |
virtual const boost::any & | value (Field field) const |
Returns the field value. More... | |
virtual WString | valueText (Field field) const |
Returns the field value text. More... | |
virtual void | setValidator (Field field, WValidator *validator) |
Sets a validator. More... | |
virtual WValidator * | validator (Field field) const |
Returns a validator. More... | |
virtual bool | validateField (Field field) |
Validates a field. More... | |
virtual void | setValidated (Field field, bool validated) |
Sets whether a field has been validated. More... | |
virtual bool | isValidated (Field field) const |
Returns whether the field has been validated yet. More... | |
const WValidator::Result & | validation (Field field) const |
Returns the result of a validation. More... | |
virtual void | setValidation (Field field, const WValidator::Result &result) |
Sets the validation result for a field. More... | |
Public Member Functions inherited from Wt::WObject | |
WObject (WObject *parent=0) | |
Create a WObject with a given parent object. More... | |
virtual | ~WObject () |
Destructor. More... | |
virtual const std::string | id () const |
Returns the (unique) identifier for this object. More... | |
virtual void | setObjectName (const std::string &name) |
Sets an object name. More... | |
virtual std::string | objectName () const |
Returns the object name. More... | |
void | resetLearnedSlots () |
Resets learned stateless slot implementations. More... | |
template<class T > | |
void | resetLearnedSlot (void(T::*method)()) |
Resets a learned stateless slot implementation. More... | |
template<class T > | |
WStatelessSlot * | implementStateless (void(T::*method)()) |
Declares a slot to be stateless and learn client-side behaviour on first invocation. More... | |
template<class T > | |
WStatelessSlot * | implementStateless (void(T::*method)(), void(T::*undoMethod)()) |
Declares a slot to be stateless and learn client-side behaviour in advance. More... | |
void | isNotStateless () |
Marks the current function as not stateless. More... | |
template<class T > | |
WStatelessSlot * | implementJavaScript (void(T::*method)(), const std::string &jsCode) |
Provides a JavaScript implementation for a method. More... | |
void | addChild (WObject *child) |
Adds a child object. More... | |
virtual void | removeChild (WObject *child) |
Removes a child object. More... | |
const std::vector< WObject * > & | children () const |
Returns the children. | |
WObject * | parent () const |
Returns the parent object. | |
Additional Inherited Members | |
Protected Member Functions inherited from Wt::WObject | |
virtual WStatelessSlot * | getStateless (Method method) |
On-demand stateless slot implementation. More... | |
Static Protected Member Functions inherited from Wt::WObject | |
static WObject * | sender () |
Returns the sender of the current slot call. More... | |
A basic model class for forms.
This implements field data and validation handling for (simple) form-based views. It provides a standard way for views to perform field validation, and react to validation results.
All fields are uniquely identified using a string literal (which is the Field type). For each field, its value, the visibility, whether the field is read-only, and its current validation status is managed by the model. In addition, you will typically specialize the class to customize the validation and application logic.
Although it can be setup to use WValidator objects for individual fields, also other validation where more entered information needs to be considered simultaneously can be implemented.
A model is typically used by a View which renders the fields configured in the model, updates the model values, invokes and reflects the validation status.
Example (a bit contrived since you will usually not use the model directly):
typedef const char* Wt::WFormModel::Field |
A type to identify a field.
Fields are identified by a string literal constant.
Wt::WFormModel::WFormModel | ( | WObject * | parent = 0 | ) |
Constructor.
Creates a new form model.
void Wt::WFormModel::addField | ( | Field | field, |
const WString & | info = WString::Empty |
||
) |
Adds a field.
The field
is added to the model, with an optional short informational message that can be used by views to provide a hint on the value that needs to be entered. The message is set as the validation message as long as the field has not yet been validated.
If the field
was already in the model, its data is reset.
std::vector< WFormModel::Field > Wt::WFormModel::fields | ( | ) | const |
Returns the fields.
This returns the fields currently configured in the model (added with addField() or for which a value or property has been set).
|
virtual |
Returns whether a field is read only.
The default implementation returns the value set by setReadOnly()
Reimplemented in Wt::Auth::RegistrationModel.
|
virtual |
Returns whether the field has been validated yet.
This is initially false
, and set to true
by setValidation().
|
virtual |
Returns whether a field is visible.
In some cases not all fields of the model need to be shown. This may depend on values input for certain fields, and thus change dynamically. You may specialize this method to indicate that a certain field should be invisible.
The default implementation returns the value set by setVisible().
Reimplemented in Wt::Auth::RegistrationModel, and Wt::Auth::AuthModel.
Returns a field label.
The default implementation returns the WString::tr(field)
Reimplemented in Wt::Auth::FormBaseModel.
void Wt::WFormModel::removeField | ( | Field | field | ) |
Removes a field.
The field
is removed from the model.
|
virtual |
Resets the model.
The default implementation clears the value of all fields, and resets the validation state to not validated.
Reimplemented in Wt::Auth::RegistrationModel, and Wt::Auth::AuthModel.
void Wt::WFormModel::setReadOnly | ( | Field | field, |
bool | readOnly | ||
) |
|
virtual |
Sets whether a field has been validated.
This is usually not used directly, but invoked by setValidation()
A field is initially (or after reset()), not validated.
|
virtual |
Sets the validation result for a field.
This will also set the field as validated.
|
virtual |
Sets a validator.
If the validator has no ownership yet, the form model will take ownership.
|
virtual |
Sets the field value.
void Wt::WFormModel::setVisible | ( | Field | field, |
bool | visible | ||
) |
Sets whether a field is visible.
Fields are visible by default. An invisible field will be ignored during validation (i.e. will be considered as valid).
bool Wt::WFormModel::valid | ( | ) | const |
Returns the current overall validation state.
This checks the validation() of all fields, and returns true
if all all fields have been validated and are valid.
|
virtual |
Validates the current input.
The default implementation calls validateField() for each field and returns true
if all fields validated.
Reimplemented in Wt::Auth::AuthModel.
|
virtual |
Validates a field.
The default implementation uses the validator configured for the field to validate the field contents, or if no validator has been configured assumes that the field is valid.
You will typically customize this method for more complex validation cases.
Reimplemented in Wt::Auth::RegistrationModel, and Wt::Auth::AuthModel.
const WValidator::Result & Wt::WFormModel::validation | ( | Field | field | ) | const |
Returns the result of a validation.
|
virtual |
Returns a validator.
Returns the validator for the field.
|
virtual |
Returns the field value.
Returns the field value text.
This uses Wt::asString() to interpret the current value as text.