cutelyst  3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorregularexpression.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorregularexpression_p.h"
7 
8 using namespace Cutelyst;
9 
11  const QRegularExpression &regex,
12  const ValidatorMessages &messages,
13  const QString &defValKey)
14  : ValidatorRule(*new ValidatorRegularExpressionPrivate(field, regex, messages, defValKey))
15 {
16 }
17 
19 {
20 }
21 
23  const ParamsMultiMap &params) const
24 {
25  ValidatorReturnType result;
26 
27  Q_D(const ValidatorRegularExpression);
28 
29  const QString v = value(params);
30 
31  if (d->regex.isValid()) {
32  if (!v.isEmpty()) {
33  if (v.contains(d->regex)) {
34  result.value.setValue(v);
35  } else {
36  result.errorMessage = validationError(c);
37  qCDebug(C_VALIDATOR,
38  "ValidatorRegularExpression: Validation failed for field %s at %s::%s "
39  "because value does not match the following regular expression: %s",
40  qPrintable(field()),
41  qPrintable(c->controllerName()),
42  qPrintable(c->actionName()),
43  qPrintable(d->regex.pattern()));
44  }
45  } else {
46  defaultValue(c, &result, "ValidatorRegularExpression");
47  }
48  } else {
50  qCWarning(C_VALIDATOR,
51  "ValidatorRegularExpression: the regular expression for the field %s at %s::%s "
52  "is not valid: %s",
53  qPrintable(field()),
54  qPrintable(c->controllerName()),
55  qPrintable(c->actionName()),
56  qPrintable(d->regex.errorString()));
57  }
58 
59  return result;
60 }
61 
63  const QVariant &errorData) const
64 {
65  QString error;
66  Q_UNUSED(errorData)
67  const QString _label = label(c);
68  if (_label.isEmpty()) {
69  error = c->translate("Cutelyst::ValidatorRegularExpression",
70  "Does not match the desired format.");
71  } else {
72  //: %1 will be replaced by the field label
73  error = c->translate("Cutelyst::ValidatorRegularExpression",
74  "The “%1” field does not match the desired format.")
75  .arg(_label);
76  }
77  return error;
78 }
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Stores custom error messages and the input field label.
ValidatorRegularExpression(const QString &field, const QRegularExpression &regex, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new regex validator.
The Cutelyst Context.
Definition: context.h:38
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:490
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
QString arg(Args &&... args) const const
~ValidatorRegularExpression() override
Deconstructs the regex validator.
void defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...
<Cutelyst/Plugins/Utils/validatorregularexpression.h>
void setValue(QVariant &&value)