Wt: Wt::WValidator Class Reference

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Classes | Public Types | Public Member Functions
Wt::WValidator Class Reference

A validator is used to validate user input according to pre-defined rules. More...

`#include <Wt/WValidator>`

Inheritance diagram for Wt::WValidator:
[legend]

List of all members.

## Classes
class | Result
| A class that holds a validation result. More...
## 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 Result | validate (const WString &input) const
| Validates the given input.
virtual WString | format () const
| Returns the validator format.
virtual void | createExtConfig (std::ostream &config) const
| Provides Ext-compatible config options for client-side validation.
virtual std::string | javaScriptValidate () const
| Creates a Javascript object that validates the input.
virtual std::string | inputFilter () const
| Returns a regular expression that filters input.

## Detailed Description

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().

### i18n

The strings used in this class can be translated by overriding the default values for the following localization keys:

- Wt.WValidator.Invalid: This field cannot be empty

See also:
  WFormWidget

## Member Enumeration Documentation

enum Wt::WValidator::State

The state in which validated input can exist.

Enumerator:

  Invalid | The input is invalid.
  InvalidEmpty | The input is invalid (empty and mandatory).
  Valid | The input is valid.

## Constructor & Destructor Documentation

Wt::WValidator::WValidator | ( | bool | mandatory,
|  | WObject * | parent = `0`
| ) |  |

Creates a new validator.

Indicate whether input is mandatory.

See also:
  setMandatory(bool)

Wt::WValidator::~WValidator | ( |  | ) |

Destructor.

The validator automatically removes itself from all formfields to which it was associated.

## Member Function Documentation

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.

WString Wt::WValidator::format | ( |  | ) | const`[virtual]`

Returns the validator format.

The default implementation returns an empty string.

Reimplemented in Wt::WDateValidator.

std::string Wt::WValidator::inputFilter | ( |  | ) | const`[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.

See also:
  javaScriptValidate()

Reimplemented in Wt::WIntValidator.

WString Wt::WValidator::invalidBlankText | ( |  | ) | const

Returns the message displayed when a mandatory field is left blank.

See also:
  setInvalidBlankText(const WString&)

std::string Wt::WValidator::javaScriptValidate | ( |  | ) | const`[virtual]`

Creates a Javascript object that validates the input.

The JavaScript expression should evaluate to an object which contains a `validate(text)` function, which returns an object that contains the following two fields:

- fields: a boolean valid,
- a `message` that indicates the problem if not valid.

Returns an empty string if the validator does not provide a client-side validation implementationq.

Note:
  The signature and contract changed changed in Wt 3.1.9.

See also:
  inputFilter()

Reimplemented in Wt::WDateValidator, Wt::WDoubleValidator, Wt::WIntValidator, Wt::WRegExpValidator, and Wt::WLengthValidator.

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::Result Wt::WValidator::validate | ( | const WString & | input | ) | const`[virtual]`

Validates the given input.

Note:
  The signature for this method changed in Wt 3.2.0.

Reimplemented in Wt::WDateValidator, Wt::Auth::AbstractPasswordService::AbstractStrengthValidator, Wt::WRegExpValidator, Wt::WDoubleValidator, Wt::WIntValidator, and Wt::WLengthValidator.

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator