cutelyst 5.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-2025 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 ParamsMultiMap &params,
52 ValidatorRtFn cb) const
53{
54 cb(validate(c, params));
55}
56
58 const QVariant &errorData) const
59{
60 Q_UNUSED(errorData)
61 const QString _label = label(c);
62 if (_label.isEmpty()) {
63 //% "Does not match the desired format."
64 return c->qtTrId("cutelyst-valregex-genvalerr");
65 } else {
66 //: %1 will be replaced by the field label
67 //% "The “%1” field does not match the desired format."
68 return c->qtTrId("cutelyst-valregex-genvalerr-label").arg(_label);
69 }
70}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:657
The field under validation must match the given regular expression.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
ValidatorRegularExpression(const QString &field, const QRegularExpression &regex, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey={})
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(const Context *c) const
QString debugString(const Context *c) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) 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.