cutelyst  4.4.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-2023 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 
21  const ParamsMultiMap &params) const
22 {
23  ValidatorReturnType result;
24 
25  Q_D(const ValidatorRegularExpression);
26 
27  const QString v = value(params);
28 
29  if (d->regex.isValid()) {
30  if (!v.isEmpty()) {
31  if (v.contains(d->regex)) {
32  result.value.setValue(v);
33  } else {
34  result.errorMessage = validationError(c);
35  qCDebug(C_VALIDATOR).noquote().nospace()
36  << debugString(c) << " value \"" << v << "\" does not match " << d->regex;
37  }
38  } else {
39  defaultValue(c, &result);
40  }
41  } else {
43  qCWarning(C_VALIDATOR).noquote().nospace()
44  << debugString(c) << " the regular expression is not valid: " << d->regex.errorString();
45  }
46 
47  return result;
48 }
49 
51  const QVariant &errorData) const
52 {
53  Q_UNUSED(errorData)
54  const QString _label = label(c);
55  if (_label.isEmpty()) {
56  //% "Does not match the desired format."
57  return c->qtTrId("cutelyst-valregex-genvalerr");
58  } else {
59  //: %1 will be replaced by the field label
60  //% "The “%1” field does not match the desired format."
61  return c->qtTrId("cutelyst-valregex-genvalerr-label").arg(_label);
62  }
63 }
The Cutelyst Context.
Definition: context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:656
The field under validation must match the given regular expression.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
ValidatorRegularExpression(const QString &field, const QRegularExpression &regex, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
The Cutelyst namespace holds all public Cutelyst API.
QString arg(Args &&... args) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
void setValue(QVariant &&value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49