Cutelyst  2.13.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 
67 {
68  QString error;
69  const QStringList vals = errorData.toStringList();
70  const QString _label = label(c);
71  if (_label.isEmpty()) {
72  //: %1 will be replaced by a comma separated list of allowed values
73  error = c->translate("Cutelyst::ValidatorIn", "Has to be one of the following values: %1").arg(c->locale().createSeparatedList(vals));
74  } else {
75  //: %1 will be replaced by the field label, %2 will be replaced by a comma separated list of allowed values
76  error = c->translate("Cutelyst::ValidatorIn", "The value in the “%1” field has to be one of the following values: %2").arg(_label, c->locale().createSeparatedList(vals));
77  }
78  return error;
79 }
80 
82 {
83  QString error;
84  Q_UNUSED(errorData)
85  const QString _label = label(c);
86  if (_label.isEmpty()) {
87  error = c->translate("Cutelyst::ValidatorIn", "The list of comparison values is empty.");
88  } else {
89  //: %1 will be replaced by the field label
90  error = c->translate("Cutelyst::ValidatorIn", "The list of comparison values for the “%1” field is empty.").arg(_label);
91  }
92  return error;
93 }
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.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
Definition: validatorin.cpp:32
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QLocale locale() const
Definition: context.cpp:457
The Cutelyst Context.
Definition: context.h:51
bool empty() const
bool isEmpty() const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:481
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.
~ValidatorIn() override
Deconstructs the in validator.
Definition: validatorin.cpp:28
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:81
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 createSeparatedList(const QStringList &list) const
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QStringList toStringList() const
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
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) 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 ...
void stash(const QVariantHash &unite)
Definition: context.h:568