Wt  3.7.1
Classes | Public Types | Public Member Functions | List of all members
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:
Inheritance graph


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 Types inherited from Wt::WObject
typedef void(WObject::* Method) ()
 Typedef for a WObject method without arguments.

Public Member Functions

 WValidator (WObject *parent=0)
 Creates a new validator.
 WValidator (bool mandatory, WObject *parent=0)
 Creates a new validator. More...
 ~WValidator ()
 Destructor. More...
void setMandatory (bool how)
 Sets if input is mandatory. More...
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. More...
WString invalidBlankText () const
 Returns the message displayed when a mandatory field is left blank. More...
virtual Result validate (const WString &input) const
 Validates the given input. More...
virtual WString format () const
 Returns the validator format. More...
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. More...
virtual std::string inputFilter () const
 Returns a regular expression that filters input. 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.
WObjectparent () 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 WObjectsender ()
 Returns the sender of the current slot call. More...

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


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

See also

Member Enumeration Documentation

◆ State

The state in which validated input can exist.


The input is invalid.


The input is invalid (empty and mandatory).


The input is valid.

Constructor & Destructor Documentation

◆ WValidator()

Wt::WValidator::WValidator ( bool  mandatory,
WObject parent = 0 

Creates a new validator.

Indicate whether input is mandatory.

See also

◆ ~WValidator()

Wt::WValidator::~WValidator ( )


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

Member Function Documentation

◆ format()

WString Wt::WValidator::format ( ) const

Returns the validator format.

The default implementation returns an empty string.

Reimplemented in Wt::WDateValidator, and Wt::WTimeValidator.

◆ inputFilter()

std::string Wt::WValidator::inputFilter ( ) const

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

Reimplemented in Wt::WIntValidator.

◆ invalidBlankText()

WString Wt::WValidator::invalidBlankText ( ) const

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

See also
setInvalidBlankText(const WString&)

◆ javaScriptValidate()

std::string Wt::WValidator::javaScriptValidate ( ) const

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.

The signature and contract changed changed in Wt 3.1.9.
See also

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

◆ setInvalidBlankText()

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

◆ setMandatory()

void Wt::WValidator::setMandatory ( bool  how)

Sets if input is mandatory.

When an input is not mandatory, then an empty field is always valid.

◆ validate()

WValidator::Result Wt::WValidator::validate ( const WString input) const

Validates the given input.

The signature for this method changed in Wt 3.2.0.

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

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13