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
8using 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
21
23 const ParamsMultiMap &params) const
24{
26
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}
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:490
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.
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 ...
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
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
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
void setValue(const T &value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.