Cutelyst  2.14.2
validatorrule.cpp
1 /*
2  * Copyright (C) 2017-2018 Matthias Fehring <kontakt@buschmann23.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "validatorrule_p.h"
20 #include <Cutelyst/Context>
21 #include <Cutelyst/ParamsMultiMap>
22 
23 using namespace Cutelyst;
24 
25 ValidatorRule::ValidatorRule(const QString &field, const ValidatorMessages &messages, const QString &defValKey) :
26  d_ptr(new ValidatorRulePrivate(field, messages, defValKey))
27 {
28 }
29 
30 ValidatorRule::ValidatorRule(ValidatorRulePrivate &dd) :
31  d_ptr(&dd)
32 {
33 }
34 
36 {
37 }
38 
39 QString ValidatorRule::field() const { Q_D(const ValidatorRule); return d->field; }
40 
42 {
43  QString v;
44 
45  Q_D(const ValidatorRule);
46 
47  if (!d->field.isEmpty() && !params.empty()) {
48  if (d->trimBefore) {
49  v = params.value(d->field).trimmed();
50  } else {
51  v = params.value(d->field);
52  }
53  }
54 
55  return v;
56 }
57 
59 {
60  QString l;
61  Q_D(const ValidatorRule);
62  if (d->messages.label) {
63  if (d->translationContext.size()) {
64  l = c->translate(d->translationContext.data(), d->messages.label);
65  } else {
66  l = QString::fromUtf8(d->messages.label);
67  }
68  }
69  return l;
70 }
71 
73 {
74  QString error;
75  Q_UNUSED(errorData)
76  Q_D(const ValidatorRule);
77  if (d->messages.validationError) {
78  if (d->translationContext.size()) {
79  error = c->translate(d->translationContext.data(), d->messages.validationError);
80  } else {
81  error = QString::fromUtf8(d->messages.validationError);
82  }
83  } else {
84  error = genericValidationError(c, errorData);
85  }
86  return error;
87 }
88 
90 {
91  QString error;
92  Q_UNUSED(errorData)
93  const QString _label = label(c);
94  if (!_label.isEmpty()) {
95  error = c->translate("Cutelyst::ValidatorRule", "The input data in the “%1” field is not acceptable.").arg(_label);
96  } else {
97  error = c->translate("Cutelyst::ValidatorRule", "The input data is not acceptable.");
98  }
99  return error;
100 }
101 
103 {
104  QString error;
105  Q_D(const ValidatorRule);
106  Q_UNUSED(errorData)
107  if (d->messages.parsingError) {
108  if (d->translationContext.size()) {
109  error = c->translate(d->translationContext.data(), d->messages.parsingError);
110  } else {
111  error = QString::fromUtf8(d->messages.parsingError);
112  }
113  } else {
114  error = genericParsingError(c, errorData);
115  }
116  return error;
117 }
118 
120 {
121  QString error;
122  Q_UNUSED(errorData)
123  const QString _label = label(c);
124  if (!_label.isEmpty()) {
125  error = c->translate("Cutelyst::ValidatorRule", "The input data in the “%1“ field could not be parsed.").arg(_label);
126  } else {
127  error = c->translate("Cutelyst::ValidatorRule", "The input data could not be parsed.");
128  }
129  return error;
130 }
131 
133 {
134  QString error;
135  Q_D(const ValidatorRule);
136  Q_UNUSED(errorData)
137  if (d->messages.validationDataError) {
138  if (d->translationContext.size()) {
139  error = c->translate(d->translationContext.data(), d->messages.validationDataError);
140  } else {
141  error = QString::fromUtf8(d->messages.validationDataError);
142  }
143  } else {
144  error = genericValidationDataError(c, errorData);
145  }
146  return error;
147 }
148 
150 {
151  QString error;
152  Q_UNUSED(errorData)
153  const QString _label = label(c);
154  if (!_label.isEmpty()) {
155  error = c->translate("Cutelyst::ValidatorRule", "Missing or invalid validation data for the “%1” field.").arg(_label);
156  } else {
157  error = c->translate("Cutelyst::ValidatorRule", "Missing or invalid validation data.");
158  }
159  return error;
160 }
161 
162 void ValidatorRule::defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const
163 {
164  Q_ASSERT_X(c, "getting default value", "invalid context object");
165  Q_ASSERT_X(result, "getting default value", "invalid result object");
166  Q_ASSERT_X(validatorName, "getting default value", "invalid validator name");
167  Q_D(const ValidatorRule);
168  if (!d->defValKey.isEmpty() && c->stash().contains(d->defValKey)) {
169  result->value.setValue(c->stash(d->defValKey));
170  qCDebug(C_VALIDATOR, "%s: Using default value \"%s\" for field %s in %s::%s.",
171  validatorName,
172  qPrintable(result->value.toString()),
173  qPrintable(field()),
174  qPrintable(c->controllerName()),
175  qPrintable(c->actionName()));
176  }
177 }
178 
179 bool ValidatorRule::trimBefore() const { Q_D(const ValidatorRule); return d->trimBefore; }
180 
181 void ValidatorRule::setTrimBefore(bool trimBefore)
182 {
183  Q_D(ValidatorRule);
184  d->trimBefore = trimBefore;
185 }
186 
187 void ValidatorRule::setTranslationContext(QLatin1String trContext)
188 {
189  Q_D(ValidatorRule);
190  d->translationContext = trContext;
191 }
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
bool trimBefore() const
Returns true if the field value should be trimmed before validation.
bool empty() const
QString field() const
Returns the name of the field to validate.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
Stores custom error messages and the input field label.
virtual QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error mesage if validation failed.
The Cutelyst Context.
Definition: context.h:51
QString fromUtf8(const char *str, int size)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
bool isEmpty() const
virtual QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if any validation data is missing or invalid.
Base class for all validator rules.
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 ...
void setValue(const T &value)
virtual QString genericParsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if an error occures while parsing input.
virtual ~ValidatorRule()
Deconstructs the ValidatorRule.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:481
Contains the result of a single input parameter validation.
Definition: validatorrule.h:62
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
QString toString() const
void stash(const QVariantHash &unite)
Definition: context.h:568
const T value(const Key &key, const T &defaultValue) const
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occured while parsing input.