cutelyst  3.9.1
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-2022 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 
11 using namespace Cutelyst;
12 
14  const ValidatorMessages &messages,
15  const QString &defValKey)
16  : d_ptr(new ValidatorRulePrivate(field, messages, defValKey))
17 {
18 }
19 
20 ValidatorRule::ValidatorRule(ValidatorRulePrivate &dd)
21  : d_ptr(&dd)
22 {
23 }
24 
26 {
27 }
28 
30 {
31  Q_D(const ValidatorRule);
32  return d->field;
33 }
34 
36 {
37  QString v;
38 
39  Q_D(const ValidatorRule);
40 
41  if (!d->field.isEmpty() && !params.empty()) {
42  if (d->trimBefore) {
43  v = params.value(d->field).trimmed();
44  } else {
45  v = params.value(d->field);
46  }
47  }
48 
49  return v;
50 }
51 
53 {
54  QString l;
55  Q_D(const ValidatorRule);
56  if (d->messages.label) {
57  if (d->translationContext.size()) {
58  l = c->translate(d->translationContext.data(), d->messages.label);
59  } else {
60  l = QString::fromUtf8(d->messages.label);
61  }
62  }
63  return l;
64 }
65 
67 {
68  QString error;
69  Q_UNUSED(errorData)
70  Q_D(const ValidatorRule);
71  if (d->messages.validationError) {
72  if (d->translationContext.size()) {
73  error = c->translate(d->translationContext.data(), d->messages.validationError);
74  } else {
75  error = QString::fromUtf8(d->messages.validationError);
76  }
77  } else {
78  error = genericValidationError(c, errorData);
79  }
80  return error;
81 }
82 
84 {
85  QString error;
86  Q_UNUSED(errorData)
87  const QString _label = label(c);
88  if (!_label.isEmpty()) {
89  error = c->translate("Cutelyst::ValidatorRule",
90  "The input data in the “%1” field is not acceptable.")
91  .arg(_label);
92  } else {
93  error = c->translate("Cutelyst::ValidatorRule", "The input data is not acceptable.");
94  }
95  return error;
96 }
97 
99 {
100  QString error;
101  Q_D(const ValidatorRule);
102  Q_UNUSED(errorData)
103  if (d->messages.parsingError) {
104  if (d->translationContext.size()) {
105  error = c->translate(d->translationContext.data(), d->messages.parsingError);
106  } else {
107  error = QString::fromUtf8(d->messages.parsingError);
108  }
109  } else {
110  error = genericParsingError(c, errorData);
111  }
112  return error;
113 }
114 
116 {
117  QString error;
118  Q_UNUSED(errorData)
119  const QString _label = label(c);
120  if (!_label.isEmpty()) {
121  error = c->translate("Cutelyst::ValidatorRule",
122  "The input data in the “%1“ field could not be parsed.")
123  .arg(_label);
124  } else {
125  error = c->translate("Cutelyst::ValidatorRule", "The input data could not be parsed.");
126  }
127  return error;
128 }
129 
131 {
132  QString error;
133  Q_D(const ValidatorRule);
134  Q_UNUSED(errorData)
135  if (d->messages.validationDataError) {
136  if (d->translationContext.size()) {
137  error = c->translate(d->translationContext.data(), d->messages.validationDataError);
138  } else {
139  error = QString::fromUtf8(d->messages.validationDataError);
140  }
141  } else {
142  error = genericValidationDataError(c, errorData);
143  }
144  return error;
145 }
146 
148 {
149  QString error;
150  Q_UNUSED(errorData)
151  const QString _label = label(c);
152  if (!_label.isEmpty()) {
153  error = c->translate("Cutelyst::ValidatorRule",
154  "Missing or invalid validation data for the “%1” field.")
155  .arg(_label);
156  } else {
157  error = c->translate("Cutelyst::ValidatorRule", "Missing or invalid validation data.");
158  }
159  return error;
160 }
161 
163  ValidatorReturnType *result,
164  const char *validatorName) const
165 {
166  Q_ASSERT_X(c, "getting default value", "invalid context object");
167  Q_ASSERT_X(result, "getting default value", "invalid result object");
168  Q_ASSERT_X(validatorName, "getting default value", "invalid validator name");
169  Q_D(const ValidatorRule);
170  if (!d->defValKey.isEmpty() && c->stash().contains(d->defValKey)) {
171  result->value.setValue(c->stash(d->defValKey));
172  qCDebug(C_VALIDATOR,
173  "%s: Using default value \"%s\" for field %s in %s::%s.",
174  validatorName,
175  qPrintable(result->value.toString()),
176  qPrintable(field()),
177  qPrintable(c->controllerName()),
178  qPrintable(c->actionName()));
179  }
180 }
181 
183 {
184  Q_D(const ValidatorRule);
185  return d->trimBefore;
186 }
187 
188 void ValidatorRule::setTrimBefore(bool trimBefore)
189 {
190  Q_D(ValidatorRule);
191  d->trimBefore = trimBefore;
192 }
193 
194 void ValidatorRule::setTranslationContext(QLatin1String trContext)
195 {
196  Q_D(ValidatorRule);
197  d->translationContext = trContext;
198 }
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
virtual QString genericParsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if an error occures while parsing input.
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occurred while parsing input.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
bool empty() const const
Stores custom error messages and the input field label.
virtual QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if any validation data is missing or invalid.
virtual QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error mesage if validation failed.
The Cutelyst Context.
Definition: context.h:38
QString fromUtf8(const char *str, int size)
void stash(const QVariantHash &unite)
Definition: context.cpp:566
bool isEmpty() const const
bool trimBefore() const
Returns true if the field value should be trimmed before validation.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:490
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
virtual ~ValidatorRule()
Deconstructs the ValidatorRule.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
QString toString() const const
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 ...
const T value(const Key &key, const T &defaultValue) const const