Wt: Wt::WLengthValidator Class Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | List of all members
Wt::WLengthValidator Class Reference

A validator that checks the string length of user input. More...

`#include <Wt/WLengthValidator.h>`

Inheritance diagram for Wt::WLengthValidator:
[legend]

## Public Member Functions
| WLengthValidator ()
| Creates a length validator that accepts input of any length.
| WLengthValidator (int minLength, int maxLength)
| Creates a length validator that accepts input within a length range.
void | setMinimumLength (int minimum)
| Sets the minimum length.
int | minimumLength () const
| Returns the minimum length.
void | setMaximumLength (int maximum)
| Sets the maximum length.
int | maximumLength () const
| Returns the maximum length.
virtual Result | validate (const WString &input) const override
| Validates the given input.
void | setInvalidTooShortText (const WString &text)
| Sets the message to display when the input is too short.
WString | invalidTooShortText () const
| Returns the message displayed when the input is too short.
void | setInvalidTooLongText (const WString &text)
| Sets the message to display when the input is too long.
WString | invalidTooLongText () const
| Returns the message displayed when the input is too long.
virtual std::string | javaScriptValidate () const override
| Creates a Javascript object that validates the input.
Public Member Functions inherited from Wt::WValidator
| WValidator (bool mandatory=false)
| Creates a new validator.
virtual | ~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 WString | format () const
| Returns the validator format.
virtual std::string | inputFilter () const
| Returns a regular expression that filters input.

## Additional Inherited Members
Public Types inherited from Wt::WValidator
typedef ValidationState | State
| Typedef for enum Wt::ValidationState.
Protected Member Functions inherited from Wt::WValidator

## Detailed Description

A validator that checks the string length of user input.

This validator checks whether user input is within the specified range of accepted string lengths.

If you only want to limit the length on a line edit, you may also use WLineEdit::setMaxLength().

Usage example:

Wt::WLineEdit *lineEdit = addWidget(std::make_unique<Wt::WLineEdit>());
auto validator = std::make_shared<Wt::WLengthValidator>(5, 15);
lineEdit->setValidator(validator);
lineEdit->setText("abcdef");
Wt::WFormWidget::setValidator
void setValidator(const std::shared_ptr< WValidator > &validator)
Sets a validator for this field.
Wt::WLineEdit
A widget that provides a single line edit.
Definition WLineEdit.h:76
Wt::WLineEdit::setText
virtual void setText(const WString &text)
Sets the content of the line edit.

### i18n

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

- Wt.WLengthValidator.TooShort: The input must be at least {1} characters
- Wt.WLengthValidator.BadRange: The input must have a length between {1} and {2} characters
- Wt.WLengthValidator.TooLong: The input must be no more than {1} characters

## Member Function Documentation

## ◆ invalidTooLongText()

WString Wt::WLengthValidator::invalidTooLongText | ( |  | ) | const

Returns the message displayed when the input is too long.

See also
  setInvalidTooLongText(const WString&)

## ◆ invalidTooShortText()

WString Wt::WLengthValidator::invalidTooShortText | ( |  | ) | const

Returns the message displayed when the input is too short.

See also
  setInvalidTooShortText(const WString&)

## ◆ javaScriptValidate()

virtual std::string Wt::WLengthValidator::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.

## ◆ maximumLength()

int Wt::WLengthValidator::maximumLength | ( |  | ) | const

Returns the maximum length.

See also
  setMaximumLength(int)

## ◆ minimumLength()

int Wt::WLengthValidator::minimumLength | ( |  | ) | const

Returns the minimum length.

See also
  setMinimumLength(int)

## ◆ setInvalidTooLongText()

void Wt::WLengthValidator::setInvalidTooLongText | ( | const WString & | text | ) |

Sets the message to display when the input is too long.

Depending on whether minimumLength() is different from zero, the default message is "The input must have a length between {1} and {2} characters" or " "The input must be no more than {2} characters".

## ◆ setInvalidTooShortText()

void Wt::WLengthValidator::setInvalidTooShortText | ( | const WString & | text | ) |

Sets the message to display when the input is too short.

Depending on whether maximumLength() is a real bound, the default message is "The input must have a length between {1} and {2} characters" or " "The input must be at least {1} characters".

## ◆ setMaximumLength()

void Wt::WLengthValidator::setMaximumLength | ( | int | maximum | ) |

Sets the maximum length.

The default value is the maximum integer value.

## ◆ setMinimumLength()

void Wt::WLengthValidator::setMinimumLength | ( | int | minimum | ) |

Sets the minimum length.

The default value is 0.

## ◆ validate()

virtual Result Wt::WLengthValidator::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 has a length within the valid range.

Reimplemented from Wt::WValidator.