cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrule.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorrule_p.h"
7
8#include <Cutelyst/Context>
9#include <Cutelyst/ParamsMultiMap>
10
11using namespace Cutelyst;
12
14 const ValidatorMessages &messages,
15 const QString &defValKey,
16 QByteArrayView validatorName)
17 : d_ptr(new ValidatorRulePrivate(field, messages, defValKey, validatorName))
18{
19}
20
21ValidatorRule::ValidatorRule(ValidatorRulePrivate &dd)
22 : d_ptr(&dd)
23{
24}
25
27
29{
30 Q_D(const ValidatorRule);
31 return d->field;
32}
33
35{
36 Q_D(const ValidatorRule);
37
38 if (!d->field.isEmpty() && !params.empty()) {
39 if (d->trimBefore) {
40 return params.value(d->field).trimmed();
41 } else {
42 return params.value(d->field);
43 }
44 }
45
46 return {};
47}
48
50{
51 Q_D(const ValidatorRule);
52
53 if (d->messages.label) {
54 return d->translationContext ? c->translate(d->translationContext, d->messages.label)
55 : c->qtTrId(d->messages.label);
56 }
57
58 return {};
59}
60
62{
63 Q_UNUSED(errorData)
64 Q_D(const ValidatorRule);
65
66 if (d->messages.validationError) {
67 return d->translationContext
68 ? c->translate(d->translationContext, d->messages.validationError)
69 : c->qtTrId(d->messages.validationError);
70 }
71
72 return genericValidationError(c, errorData);
73}
74
76{
77 Q_UNUSED(errorData)
78 const QString _label = label(c);
79 if (_label.isEmpty()) {
80 //% "The input data is not acceptable."
81 return c->qtTrId("cutelyst-valrule-genvalerr");
82 } else {
83 //: %1 will be replaced by the field label
84 //% "The input data in the “%1” field is not acceptable."
85 return c->qtTrId("cutelyst-valrule-genvalerr-label").arg(_label);
86 }
87}
88
90{
91 Q_D(const ValidatorRule);
92 Q_UNUSED(errorData)
93
94 if (d->messages.parsingError) {
95 return d->translationContext ? c->translate(d->translationContext, d->messages.parsingError)
96 : c->qtTrId(d->messages.parsingError);
97 }
98
99 return genericParsingError(c, errorData);
100}
101
103{
104 Q_UNUSED(errorData)
105 const QString _label = label(c);
106 if (_label.isEmpty()) {
107 //% "The input data could not be parsed."
108 return c->qtTrId("cutelyst-valrule-genparseerr");
109 } else {
110 //: %1 will be replaced by the field label
111 //% "The input data in the “%1“ field could not be parsed."
112 return c->qtTrId("cutelyst-valrule-genparseerr-label").arg(_label);
113 }
114}
115
117{
118 Q_D(const ValidatorRule);
119 Q_UNUSED(errorData)
120
121 if (d->messages.validationDataError) {
122 return d->translationContext
123 ? c->translate(d->translationContext, d->messages.validationDataError)
124 : c->qtTrId(d->messages.validationDataError);
125 }
126
127 return genericValidationDataError(c, errorData);
128}
129
131{
132 Q_UNUSED(errorData)
133 const QString _label = label(c);
134 if (_label.isEmpty()) {
135 //% "Missing or invalid validation data."
136 return c->qtTrId("cutelyst-valrule-genvaldataerr");
137 } else {
138 //: %1 will be replaced by the field label
139 //% "Missing or invalid validation data for the “%1” field."
140 return c->qtTrId("cutelyst-valrule-genvaldataerr-label").arg(_label);
141 }
142}
143
145{
146 Q_ASSERT_X(c, "getting default value", "invalid context object");
147 Q_ASSERT_X(result, "getting default value", "invalid result object");
148 Q_D(const ValidatorRule);
149 if (!d->defValKey.isEmpty() && c->stash().contains(d->defValKey)) {
150 result->value.setValue(c->stash(d->defValKey));
151 qCDebug(C_VALIDATOR).noquote().nospace()
152 << d->validatorName << ": Using default value " << result->value << " for field \""
153 << field() << "\" at " << c->controllerName() << "::" << c->actionName();
154 }
155}
156
158{
159 Q_D(const ValidatorRule);
160 return QString::fromLatin1(d->validatorName) +
161 QLatin1String(": Validation failed for field \"") + field() + QLatin1String("\" at ") +
162 c->controllerName() + QLatin1String("::") + c->actionName() + QLatin1Char(':');
163}
164
165bool ValidatorRule::trimBefore() const noexcept
166{
167 Q_D(const ValidatorRule);
168 return d->trimBefore;
169}
170
171void ValidatorRule::setTrimBefore(bool trimBefore) noexcept
172{
173 Q_D(ValidatorRule);
174 d->trimBefore = trimBefore;
175}
176
177void ValidatorRule::setTranslationContext(const char *trContext) noexcept
178{
179 Q_D(ValidatorRule);
180 d->translationContext = trContext;
181}
The Cutelyst Context.
Definition context.h:42
void stash(const QVariantHash &unite)
Definition context.cpp:562
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition context.cpp:484
QString qtTrId(const char *id, int n=-1) const
Definition context.h:656
QString actionName
Definition context.h:51
QString controllerName
Definition context.h:79
Base class for all validator rules.
QString field() const noexcept
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
virtual QString genericValidationDataError(Context *c, const QVariant &errorData={}) const
virtual QString genericValidationError(Context *c, const QVariant &errorData={}) const
virtual ~ValidatorRule()
Deconstructs the ValidatorRule.
bool trimBefore() const noexcept
ValidatorRule(const QString &field, const ValidatorMessages &messages={}, const QString &defValKey={}, QByteArrayView validatorName=nullptr)
QString validationDataError(Context *c, const QVariant &errorData={}) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString parsingError(Context *c, const QVariant &errorData={}) const
virtual QString genericParsingError(Context *c, const QVariant &errorData={}) const
QString debugString(Context *c) const
The Cutelyst namespace holds all public Cutelyst API.
bool empty() const const
T value(const Key &key, const T &defaultValue) const const
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
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.