cutelyst 4.0.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
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
19
21 const ParamsMultiMap &params) const
22{
24
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 QString error;
54 Q_UNUSED(errorData)
55 const QString _label = label(c);
56 if (_label.isEmpty()) {
57 error = c->translate("Cutelyst::ValidatorRegularExpression",
58 "Does not match the desired format.");
59 } else {
60 //: %1 will be replaced by the field label
61 error = c->translate("Cutelyst::ValidatorRegularExpression",
62 "The “%1” field does not match the desired format.")
63 .arg(_label);
64 }
65 return error;
66}
The Cutelyst Context.
Definition: context.h:38
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
<Cutelyst/Plugins/Utils/validatorregularexpression.h>
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.
void defaultValue(Context *c, ValidatorReturnType *result) 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.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
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