A validator is used to validate user input according to pre-defined rules. More...
#include <Wt/WValidator>
Public Types | |
enum | State { Invalid, InvalidEmpty, Valid } |
The state in which validated input can exist. More... | |
Public Member Functions | |
WValidator (WObject *parent=0) | |
Creates a new validator. | |
WValidator (bool mandatory, WObject *parent=0) | |
Creates a new validator. | |
~WValidator () | |
Destructor. | |
void | setMandatory (bool how) |
Sets if input is mandatory. | |
bool | isMandatory () const |
Returns if input is mandatory. | |
void | setInvalidBlankText (const WString &text) |
Sets the message to display when a mandatory field is left blank. | |
WString | invalidBlankText () const |
Returns the message displayed when a mandatory field is left blank. | |
virtual void | fixup (WString &input) const |
This function attempts to change input to be valid according to the validator's rules. | |
virtual State | validate (WString &input) const |
Validates the given input. | |
virtual void | createExtConfig (std::ostream &config) const |
Provides Ext-compatible config options for client-side validation. | |
Protected Member Functions | |
virtual std::string | javaScriptValidate (const std::string &jsRef) const |
Creates a Javascript expression that validates the input. | |
virtual std::string | inputFilter () const |
Returns a regular expression that filters input. |
A validator is used to validate user input according to pre-defined rules.
A validator may be associated with a form widget using WFormWidget::setValidator().
The validator validates the user input. A validator may have a split implementation to provide both validation at the client-side (which gives instant feed-back to the user while editing), and server-side validation (to be sure that the client was not tampered with). The feed-back given by (client-side and server-side) validation is reflected in the style class of the form field: a style class of Wt-invalid
is set for a field that is invalid.
This WValidator only checks that mandatory fields are not empty. This class is reimplemented in WDateValidator, WIntValidator, WDoubleValidator, WLengthValidator and WRegExpValidator. All these validators provibe both client-side and server-side validation.
If these validators are not suitable, you can inherit from this class, and provide a suitable implementation to validate() which does the server-side validation. If you want to provide client-side validation for your own validator, you may also reimplement javaScriptValidate().
Wt::WValidator::WValidator | ( | bool | mandatory, |
WObject * | parent = 0 |
||
) |
Wt::WValidator::~WValidator | ( | ) |
Destructor.
The validator automatically removes itself from all formfields to which it was associated.
void Wt::WValidator::fixup | ( | WString & | input ) | const [virtual] |
This function attempts to change input to be valid according to the validator's rules.
In general the function needs not to change the input into a valid input. The default implementation does nothing. But it may help the user in getting its input right.
std::string Wt::WValidator::inputFilter | ( | ) | const [protected, virtual] |
Returns a regular expression that filters input.
The returned regular expression is used to filter keys presses. The regular expression should accept valid single characters.
For details on valid regular expressions, see WRegExpValidator. As an example, "[0-9]" would only accept numbers as valid input.
The default implementation returns an empty string, which does not filter any input.
Reimplemented in Wt::WIntValidator.
WString Wt::WValidator::invalidBlankText | ( | ) | const |
Returns the message displayed when a mandatory field is left blank.
std::string Wt::WValidator::javaScriptValidate | ( | const std::string & | jsRef ) | const [protected, virtual] |
Creates a Javascript expression that validates the input.
The JavaScript expression should evaluate to an object with two fields: a boolean valid, and a message
that indicates the problem if not valid.
Return an empty string if you are not provide the client-side validation.
Reimplemented in Wt::WDateValidator, Wt::WDoubleValidator, Wt::WIntValidator, Wt::WLengthValidator, and Wt::WRegExpValidator.
void Wt::WValidator::setInvalidBlankText | ( | const WString & | text ) |
Sets the message to display when a mandatory field is left blank.
The default value is "This field cannot be empty".
void Wt::WValidator::setMandatory | ( | bool | how ) |
Sets if input is mandatory.
When an input is not mandatory, then an empty field is always valid.
WValidator::State Wt::WValidator::validate | ( | WString & | input ) | const [virtual] |
Validates the given input.
This function returns the current validation state of the input. The default implementation only checks whether a mandatory field is not left blank.
Reimplemented in Wt::WDateValidator, Wt::WDoubleValidator, Wt::WIntValidator, Wt::WLengthValidator, and Wt::WRegExpValidator.