cutelyst  3.7.0
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 
10 ValidatorRegularExpression::ValidatorRegularExpression(const QString &field, const QRegularExpression &regex, const ValidatorMessages &messages, const QString &defValKey) :
11  ValidatorRule(*new ValidatorRegularExpressionPrivate(field, regex, messages, defValKey))
12 {
13 }
14 
16 {
17 }
18 
20 {
21  ValidatorReturnType result;
22 
23  Q_D(const ValidatorRegularExpression);
24 
25  const QString v = value(params);
26 
27  if (d->regex.isValid()) {
28  if (!v.isEmpty()) {
29  if (v.contains(d->regex)) {
30  result.value.setValue(v);
31  } else {
32  result.errorMessage = validationError(c);
33  qCDebug(C_VALIDATOR, "ValidatorRegularExpression: Validation failed for field %s at %s::%s because value does not match the following regular expression: %s", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), qPrintable(d->regex.pattern()));
34  }
35  } else {
36  defaultValue(c, &result, "ValidatorRegularExpression");
37  }
38  } else {
40  qCWarning(C_VALIDATOR, "ValidatorRegularExpression: the regular expression for the field %s at %s::%s is not valid: %s", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), qPrintable(d->regex.errorString()));
41  }
42 
43  return result;
44 }
45 
46 QString ValidatorRegularExpression::genericValidationError(Context *c, const QVariant &errorData) const
47 {
48  QString error;
49  Q_UNUSED(errorData)
50  const QString _label = label(c);
51  if (_label.isEmpty()) {
52  error = c->translate("Cutelyst::ValidatorRegularExpression", "Does not match the desired format.");
53  } else {
54  //: %1 will be replaced by the field label
55  error = c->translate("Cutelyst::ValidatorRegularExpression", "The “%1” field does not match the desired format.").arg(_label);
56  }
57  return error;
58 }
The Cutelyst Context.
Definition: context.h:39
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
The field under validation must match the given regular expression.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
~ValidatorRegularExpression() override
Deconstructs the regex validator.
ValidatorRegularExpression(const QString &field, const QRegularExpression &regex, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new regex validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString field() const
Returns the name of the field to validate.
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 ...
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49