Cutelyst  3.5.0
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 
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 
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 
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 
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 
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 
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 
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 
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 }
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 occured 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:540
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:471
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 field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
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