Wt  4.10.4
Public Member Functions | List of all members
Wt::WDateValidator Class Reference

A validator for date input. More...

#include <Wt/WDateValidator.h>

Inheritance diagram for Wt::WDateValidator:
[legend]

Public Member Functions

 WDateValidator ()
 Creates a date validator. More...
 
 WDateValidator (const WDate &bottom, const WDate &top)
 Creates a date validator. More...
 
 WDateValidator (const WString &format)
 Creates a date validator. More...
 
 WDateValidator (const WString &format, const WDate &bottom, const WDate &top)
 Creates a date validator. More...
 
void setBottom (const WDate &bottom)
 Sets the bottom of the valid date range. More...
 
const WDatebottom () const
 Returns the bottom date of the valid range.
 
void setTop (const WDate &top)
 Sets the top of the valid date range. More...
 
const WDatetop () const
 Returns the top date of the valid range.
 
void setFormat (const WString &format)
 Sets the date format used to parse date strings. More...
 
virtual WString format () const override
 Returns the format string used to parse date strings. More...
 
void setFormats (const std::vector< WString > &formats)
 Sets the date formats used to parse date strings.
 
const std::vector< WString > & formats () const
 Returns the date formats used to parse date strings.
 
virtual Result validate (const WString &input) const override
 Validates the given input. More...
 
void setInvalidNotADateText (const WString &text)
 Sets the message to display when the input is not a date. More...
 
WString invalidNotADateText () const
 Returns the message displayed when the input is not a date. More...
 
void setInvalidTooEarlyText (const WString &text)
 Sets the message to display when the date is earlier than bottom. More...
 
WString invalidTooEarlyText () const
 Returns the message displayed when date is too early. More...
 
void setInvalidTooLateText (const WString &text)
 Sets the message to display when the date is later than top. More...
 
WString invalidTooLateText () const
 Returns the message displayed when the date is too late. More...
 
virtual std::string javaScriptValidate () const override
 Creates a Javascript object that validates the input. More...
 
- Public Member Functions inherited from Wt::WValidator
 WValidator (bool mandatory=false)
 Creates a new validator. More...
 
virtual ~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 std::string inputFilter () const
 Returns a regular expression that filters input. More...
 

Additional Inherited Members

- Public Types inherited from Wt::WValidator
typedef ValidationState State
 Typedef for enum Wt::ValidationState.
 

Detailed Description

A validator for date input.

This validator accepts input in the given date format, and optionally checks if the date is within a given range.

The format string used for validating user input are the same as those used by WDate::fromString().

Usage example:

Wt::WLineEdit *lineEdit = addWidget(std::make_unique<Wt::WLineEdit>());
auto validator = std::make_shared<Wt::WDateValidator>();
validator->setFormat("dd-MM-yyyy");
lineEdit->setValidator(validator);
lineEdit->setText("01-03-2008");
void setValidator(const std::shared_ptr< WValidator > &validator)
Sets a validator for this field.
Definition: WFormWidget.C:324
A widget that provides a single line edit.
Definition: WLineEdit.h:76
virtual void setText(const WString &text)
Sets the content of the line edit.
Definition: WLineEdit.C:55

i18n

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

Constructor & Destructor Documentation

◆ WDateValidator() [1/4]

Wt::WDateValidator::WDateValidator ( )

Creates a date validator.

The validator will accept dates using the current locale's format.

See also
WLocale::currentLocale()

◆ WDateValidator() [2/4]

Wt::WDateValidator::WDateValidator ( const WDate bottom,
const WDate top 
)

Creates a date validator.

The validator will accept dates in the indicated range using the current locale's format.

See also
WLocale::currentLocale()

◆ WDateValidator() [3/4]

Wt::WDateValidator::WDateValidator ( const WString format)

Creates a date validator.

The validator will accept dates in the date format format.

The syntax for format is as in WDate::fromString()

◆ WDateValidator() [4/4]

Wt::WDateValidator::WDateValidator ( const WString format,
const WDate bottom,
const WDate top 
)

Creates a date validator.

The validator will accept only dates within the indicated range bottom to top, in the date format format.

The syntax for format is as in WDate::fromString()

Member Function Documentation

◆ format()

virtual WString Wt::WDateValidator::format ( ) const
overridevirtual

Returns the format string used to parse date strings.

See also
setFormat()

Reimplemented from Wt::WValidator.

◆ invalidNotADateText()

WString Wt::WDateValidator::invalidNotADateText ( ) const

Returns the message displayed when the input is not a date.

See also
setInvalidNotADateText(const WString&)

◆ invalidTooEarlyText()

WString Wt::WDateValidator::invalidTooEarlyText ( ) const

Returns the message displayed when date is too early.

See also
setInvalidTooEarlyText(const WString&)

◆ invalidTooLateText()

WString Wt::WDateValidator::invalidTooLateText ( ) const

Returns the message displayed when the date is too late.

See also
setInvalidTooLateText(const WString&)

◆ javaScriptValidate()

std::string Wt::WDateValidator::javaScriptValidate ( ) const
overridevirtual

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.

See also
inputFilter()

Reimplemented from Wt::WValidator.

◆ setBottom()

void Wt::WDateValidator::setBottom ( const WDate bottom)

Sets the bottom of the valid date range.

The default is a null date constructed using WDate().

◆ setFormat()

void Wt::WDateValidator::setFormat ( const WString format)

Sets the date format used to parse date strings.

See also
WDate::fromString()

◆ setInvalidNotADateText()

void Wt::WDateValidator::setInvalidNotADateText ( const WString text)

Sets the message to display when the input is not a date.

The default message is "The date must be of the format {1}", with as first argument the format string.

◆ setInvalidTooEarlyText()

void Wt::WDateValidator::setInvalidTooEarlyText ( const WString text)

Sets the message to display when the date is earlier than bottom.

Depending on whether bottom() and top() are defined (see WDate::isNull()), the default message is "The date must be between {1} and {2}" or "The date must be after {1}".

◆ setInvalidTooLateText()

void Wt::WDateValidator::setInvalidTooLateText ( const WString text)

Sets the message to display when the date is later than top.

Depending on whether bottom() and top() are defined, the default message is "The date must be between {1} and {2}" or "The date must be before {2}".

◆ setTop()

void Wt::WDateValidator::setTop ( const WDate top)

Sets the top of the valid date range.

The default is a null date constructed using WDate().

◆ validate()

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

Validates the given input.

The input is considered valid only when it is blank for a non-mandatory field, or represents a date in the given format, and within the valid range.

Reimplemented from Wt::WValidator.