Coverage Report

Created: 2025-07-23 06:40

/src/wt/src/Wt/WRegExpValidator.h
Line
Count
Source (jump to first uncovered line)
1
// This may look like C code, but it's really -*- C++ -*-
2
/*
3
 * Copyright (C) 2008 Emweb bv, Herent, Belgium.
4
 *
5
 * See the LICENSE file for terms of use.
6
 */
7
#ifndef WREGEXPVALIDATOR_H_
8
#define WREGEXPVALIDATOR_H_
9
10
#include <limits>
11
#include <regex>
12
13
#include <Wt/WValidator.h>
14
15
namespace Wt {
16
17
#ifdef WT_TARGET_JAVA
18
struct RegExpFlag { };
19
#endif // WT_TARGET_JAVA
20
21
class WRegExp;
22
23
/*! \class WRegExpValidator Wt/WRegExpValidator.h Wt/WRegExpValidator.h
24
 *  \brief A validator that checks user input against a regular expression.
25
 *
26
 * This validator checks whether user input matches the given regular
27
 * expression. It checks the complete input; prefix ^ and suffix $ are
28
 * not needed.
29
 *
30
 * The regex should be specified using ECMAScript syntax
31
 * (http://en.cppreference.com/w/cpp/regex/ecmascript)
32
 *
33
 * Usage example:
34
 * \if cpp
35
 * \code
36
 * Wt::WLineEdit *lineEdit = addWidget(std::make_unique<Wt::WLineEdit>());
37
 * // an email address validator
38
 * auto validator = std::make_unique<Wt::WRegExpValidator>("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}");
39
 * lineEdit->setValidator(validator);
40
 * lineEdit->setText("koen@emweb.be");
41
 * \endcode
42
 * \elseif java
43
 * \code
44
 * WLineEdit lineEdit = new WLineEdit(this);
45
 * // an email address validator
46
 * WRegExpValidator validator = new WRegExpValidator("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}");
47
 * lineEdit.setValidator(validator);
48
 * lineEdit.setText("pieter@emweb.be");
49
 * \endcode
50
 * \endif
51
 *
52
 * \note This validator does not fully support unicode: it matches on the
53
 * CharEncoding::UTF8-encoded representation of the string.
54
 *
55
 * <h3>i18n</h3>
56
 *
57
 * The strings used in this class can be translated by overriding
58
 * the default values for the following localization keys:
59
 * - Wt.WRegExpValidator.Invalid: Invalid input
60
 */
61
class WT_API WRegExpValidator : public WValidator
62
{
63
public:
64
  /*! \brief Sets a new regular expression validator.
65
   */
66
  WRegExpValidator();
67
68
  /*! \brief Sets a new regular expression validator that accepts input
69
   *         that matches the given regular expression.
70
   *
71
   * This constructs a validator that matches the regular expression
72
   * \p expr.
73
   */
74
  WRegExpValidator(const WT_USTRING& pattern);
75
76
  /*! \brief Destructor.
77
   */
78
  ~WRegExpValidator();
79
80
  /*! \brief Sets the regular expression for valid input.
81
   *
82
   * Sets the ECMAscript regular expression \p expr.
83
   */
84
  void setRegExp(const WT_USTRING& pattern);
85
86
  /*! \brief Returns the regular expression for valid input.
87
   *
88
   * Returns the ECMAScript regular expression.
89
   */
90
0
  WT_USTRING regExpPattern() const { return pattern_; }
91
92
  /*! \brief Returns the regular expression for valid input.
93
   */
94
0
  std::regex regExp() const { return regex_; }
95
96
  /*! \brief Sets regular expression matching flags.
97
   */
98
  void setFlags(WFlags<RegExpFlag> flags);
99
100
  /*! \brief Returns regular expression matching flags.
101
   */
102
  WFlags<RegExpFlag> flags() const;
103
104
  /*! \brief Validates the given input.
105
   *
106
   * The input is considered valid only when it is blank for a
107
   * non-mandatory field, or matches the regular expression.
108
   */
109
  virtual Result validate(const WT_USTRING& input) const override;
110
111
  /*! \brief Sets the message to display when the input does not match.
112
   *
113
   * The default value is "Invalid input".
114
   */
115
  void setInvalidNoMatchText(const WString& text);
116
117
  /*! \brief Returns the message displayed when the input does not match.
118
   *
119
   * \sa setInvalidNoMatchText(const WString&)
120
   */
121
  WString invalidNoMatchText() const;
122
123
  virtual std::string javaScriptValidate() const override;
124
125
private:
126
  WT_USTRING pattern_;
127
  std::regex regex_;
128
  WString noMatchText_;
129
  static void loadJavaScript(WApplication *app);
130
};
131
132
}
133
134
#endif // WREGEXPVALIDATOR_H_