cutelyst  3.7.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-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorrule_p.h"
7 #include <Cutelyst/Context>
8 #include <Cutelyst/ParamsMultiMap>
9 
10 using namespace Cutelyst;
11 
12 ValidatorRule::ValidatorRule(const QString &field, const ValidatorMessages &messages, const QString &defValKey) :
13  d_ptr(new ValidatorRulePrivate(field, messages, defValKey))
14 {
15 }
16 
17 ValidatorRule::ValidatorRule(ValidatorRulePrivate &dd) :
18  d_ptr(&dd)
19 {
20 }
21 
23 {
24 }
25 
26 QString ValidatorRule::field() const { Q_D(const ValidatorRule); return d->field; }
27 
28 QString ValidatorRule::value(const Cutelyst::ParamsMultiMap &params) const
29 {
30  QString v;
31 
32  Q_D(const ValidatorRule);
33 
34  if (!d->field.isEmpty() && !params.empty()) {
35  if (d->trimBefore) {
36  v = params.value(d->field).trimmed();
37  } else {
38  v = params.value(d->field);
39  }
40  }
41 
42  return v;
43 }
44 
45 QString ValidatorRule::label(Context *c) const
46 {
47  QString l;
48  Q_D(const ValidatorRule);
49  if (d->messages.label) {
50  if (d->translationContext.size()) {
51  l = c->translate(d->translationContext.data(), d->messages.label);
52  } else {
53  l = QString::fromUtf8(d->messages.label);
54  }
55  }
56  return l;
57 }
58 
59 QString ValidatorRule::validationError(Cutelyst::Context *c, const QVariant &errorData) const
60 {
61  QString error;
62  Q_UNUSED(errorData)
63  Q_D(const ValidatorRule);
64  if (d->messages.validationError) {
65  if (d->translationContext.size()) {
66  error = c->translate(d->translationContext.data(), d->messages.validationError);
67  } else {
68  error = QString::fromUtf8(d->messages.validationError);
69  }
70  } else {
71  error = genericValidationError(c, errorData);
72  }
73  return error;
74 }
75 
76 QString ValidatorRule::genericValidationError(Context *c, const QVariant &errorData) const
77 {
78  QString error;
79  Q_UNUSED(errorData)
80  const QString _label = label(c);
81  if (!_label.isEmpty()) {
82  error = c->translate("Cutelyst::ValidatorRule", "The input data in the “%1” field is not acceptable.").arg(_label);
83  } else {
84  error = c->translate("Cutelyst::ValidatorRule", "The input data is not acceptable.");
85  }
86  return error;
87 }
88 
89 QString ValidatorRule::parsingError(Cutelyst::Context *c, const QVariant &errorData) const
90 {
91  QString error;
92  Q_D(const ValidatorRule);
93  Q_UNUSED(errorData)
94  if (d->messages.parsingError) {
95  if (d->translationContext.size()) {
96  error = c->translate(d->translationContext.data(), d->messages.parsingError);
97  } else {
98  error = QString::fromUtf8(d->messages.parsingError);
99  }
100  } else {
101  error = genericParsingError(c, errorData);
102  }
103  return error;
104 }
105 
106 QString ValidatorRule::genericParsingError(Cutelyst::Context *c, const QVariant &errorData) const
107 {
108  QString error;
109  Q_UNUSED(errorData)
110  const QString _label = label(c);
111  if (!_label.isEmpty()) {
112  error = c->translate("Cutelyst::ValidatorRule", "The input data in the “%1“ field could not be parsed.").arg(_label);
113  } else {
114  error = c->translate("Cutelyst::ValidatorRule", "The input data could not be parsed.");
115  }
116  return error;
117 }
118 
119 QString ValidatorRule::validationDataError(Context *c, const QVariant &errorData) const
120 {
121  QString error;
122  Q_D(const ValidatorRule);
123  Q_UNUSED(errorData)
124  if (d->messages.validationDataError) {
125  if (d->translationContext.size()) {
126  error = c->translate(d->translationContext.data(), d->messages.validationDataError);
127  } else {
128  error = QString::fromUtf8(d->messages.validationDataError);
129  }
130  } else {
131  error = genericValidationDataError(c, errorData);
132  }
133  return error;
134 }
135 
136 QString ValidatorRule::genericValidationDataError(Context *c, const QVariant &errorData) const
137 {
138  QString error;
139  Q_UNUSED(errorData)
140  const QString _label = label(c);
141  if (!_label.isEmpty()) {
142  error = c->translate("Cutelyst::ValidatorRule", "Missing or invalid validation data for the “%1” field.").arg(_label);
143  } else {
144  error = c->translate("Cutelyst::ValidatorRule", "Missing or invalid validation data.");
145  }
146  return error;
147 }
148 
149 void ValidatorRule::defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const
150 {
151  Q_ASSERT_X(c, "getting default value", "invalid context object");
152  Q_ASSERT_X(result, "getting default value", "invalid result object");
153  Q_ASSERT_X(validatorName, "getting default value", "invalid validator name");
154  Q_D(const ValidatorRule);
155  if (!d->defValKey.isEmpty() && c->stash().contains(d->defValKey)) {
156  result->value.setValue(c->stash(d->defValKey));
157  qCDebug(C_VALIDATOR, "%s: Using default value \"%s\" for field %s in %s::%s.",
158  validatorName,
159  qPrintable(result->value.toString()),
160  qPrintable(field()),
161  qPrintable(c->controllerName()),
162  qPrintable(c->actionName()));
163  }
164 }
165 
166 bool ValidatorRule::trimBefore() const { Q_D(const ValidatorRule); return d->trimBefore; }
167 
168 void ValidatorRule::setTrimBefore(bool trimBefore)
169 {
170  Q_D(ValidatorRule);
171  d->trimBefore = trimBefore;
172 }
173 
174 void ValidatorRule::setTranslationContext(QLatin1String trContext)
175 {
176  Q_D(ValidatorRule);
177  d->translationContext = trContext;
178 }
The Cutelyst Context.
Definition: context.h:39
void stash(const QVariantHash &unite)
Definition: context.cpp:546
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
Base class for all validator rules.
virtual QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error mesage 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.
bool trimBefore() const
Returns true if the field value should be trimmed before validation.
virtual QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if any validation data is missing or invalid.
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occured while parsing input.
virtual ~ValidatorRule()
Deconstructs the ValidatorRule.
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.
virtual QString genericParsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if an error occures while parsing input.
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
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49