Cutelyst  2.3.0
validatorin.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 "validatorin_p.h"
20 
21 using namespace Cutelyst;
22 
23 ValidatorIn::ValidatorIn(const QString &field, const QVariant &values, Qt::CaseSensitivity cs, const Cutelyst::ValidatorMessages &messages, const QString &defValKey) :
24  ValidatorRule(*new ValidatorInPrivate(field, values, cs, messages, defValKey))
25 {
26 }
27 
29 {
30 }
31 
33 {
34  ValidatorReturnType result;
35 
36  Q_D(const ValidatorIn);
37 
38  const QString v = value(params);
39  if (!v.isEmpty()) {
40  QStringList vals;
41 
42  if (d->values.type() == QVariant::StringList) {
43  vals = d->values.toStringList();
44  } else if (d->values.type() == QVariant::String) {
45  vals = c->stash(d->values.toString()).toStringList();
46  }
47 
48  if (vals.empty()) {
49  qCWarning(C_VALIDATOR, "ValidatorIn: The list of comparison values for the field %s at %s::%s is empty.", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
51  } else {
52  if (vals.contains(v, d->cs)) {
53  result.value.setValue<QString>(v);
54  } else {
55  qCDebug(C_VALIDATOR, "ValidatorIn: Validation failed for field %s at %s::%s: \"%s\" is not part of the list of comparison values.", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), qPrintable(v));
56  result.errorMessage = validationError(c, vals);
57  }
58  }
59  } else {
60  defaultValue(c, &result, "ValidatorIn");
61  }
62 
63  return result;
64 }
65 
66 QString ValidatorIn::genericValidationError(Context *c, const QVariant &errorData) const
67 {
68  QString error;
69  const QStringList vals = errorData.toStringList();
70  const QString _label = label(c);
71  if (_label.isEmpty()) {
72  error = c->translate("Cutelyst::ValidatorIn", "Has to be one of the following: %1").arg(c->locale().createSeparatedList(vals));
73  } else {
74  error = c->translate("Cutelyst::ValidatorIn", "The value in the “%1” field has to be one of the following: %2").arg(_label, c->locale().createSeparatedList(vals));
75  }
76  return error;
77 }
78 
79 QString ValidatorIn::genericValidationDataError(Context *c, const QVariant &errorData) const
80 {
81  QString error;
82  Q_UNUSED(errorData);
83  const QString _label = label(c);
84  if (_label.isEmpty()) {
85  error = c->translate("Cutelyst::ValidatorIn", "The list of comparison values is empty.");
86  } else {
87  error = c->translate("Cutelyst::ValidatorIn", "The list of comparison values for the “%1” field is empty.").arg(_label);
88  }
89  return error;
90 }
QMap< QString, QString > ParamsMultiMap
ValidatorIn(const QString &field, const QVariant &values, Qt::CaseSensitivity cs=Qt::CaseSensitive, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new in validator.
Definition: validatorin.cpp:23
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Stores custom error messages and the input field label.
~ValidatorIn()
Deconstructs the in validator.
Definition: validatorin.cpp:28
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
Definition: validatorin.cpp:32
QLocale locale() const
Definition: context.cpp:390
The Cutelyst Context.
Definition: context.h:50
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:414
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 genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error messages if the list of comparison values is empty.
Definition: validatorin.cpp:79
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Definition: validatorin.cpp:66
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString field() const
Returns the name of the field to validate.
Checks if the field value is one from a list of values.
Definition: validatorin.h:45
Contains the result of a single input parameter validation.
Definition: validatorrule.h:62
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 stash(const QVariantHash &unite)
Definition: context.h:515