cutelyst  3.7.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorin.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorin_p.h"
7 
8 using namespace Cutelyst;
9 
10 ValidatorIn::ValidatorIn(const QString &field, const QVariant &values, Qt::CaseSensitivity cs, const Cutelyst::ValidatorMessages &messages, const QString &defValKey) :
11  ValidatorRule(*new ValidatorInPrivate(field, values, cs, messages, defValKey))
12 {
13 }
14 
16 {
17 }
18 
20 {
21  ValidatorReturnType result;
22 
23  Q_D(const ValidatorIn);
24 
25  const QString v = value(params);
26  if (!v.isEmpty()) {
27  QStringList vals;
28 
29  if (d->values.userType() == QMetaType::QStringList) {
30  vals = d->values.toStringList();
31  } else if (d->values.userType() == QMetaType::QString) {
32  vals = c->stash(d->values.toString()).toStringList();
33  }
34 
35  if (vals.empty()) {
36  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()));
38  } else {
39  if (vals.contains(v, d->cs)) {
40  result.value.setValue(v);
41  } else {
42  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));
43  result.errorMessage = validationError(c, vals);
44  }
45  }
46  } else {
47  defaultValue(c, &result, "ValidatorIn");
48  }
49 
50  return result;
51 }
52 
53 QString ValidatorIn::genericValidationError(Context *c, const QVariant &errorData) const
54 {
55  QString error;
56  const QStringList vals = errorData.toStringList();
57  const QString _label = label(c);
58  if (_label.isEmpty()) {
59  //: %1 will be replaced by a comma separated list of allowed values
60  error = c->translate("Cutelyst::ValidatorIn", "Has to be one of the following values: %1").arg(c->locale().createSeparatedList(vals));
61  } else {
62  //: %1 will be replaced by the field label, %2 will be replaced by a comma separated list of allowed values
63  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));
64  }
65  return error;
66 }
67 
68 QString ValidatorIn::genericValidationDataError(Context *c, const QVariant &errorData) const
69 {
70  QString error;
71  Q_UNUSED(errorData)
72  const QString _label = label(c);
73  if (_label.isEmpty()) {
74  error = c->translate("Cutelyst::ValidatorIn", "The list of comparison values is empty.");
75  } else {
76  //: %1 will be replaced by the field label
77  error = c->translate("Cutelyst::ValidatorIn", "The list of comparison values for the “%1” field is empty.").arg(_label);
78  }
79  return error;
80 }
The Cutelyst Context.
Definition: context.h:39
void stash(const QVariantHash &unite)
Definition: context.cpp:546
QLocale locale() const noexcept
Definition: context.cpp:453
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
Checks if the field value is one from a list of values.
Definition: validatorin.h:33
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
Definition: validatorin.cpp:19
~ValidatorIn() override
Deconstructs the in validator.
Definition: validatorin.cpp:15
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:10
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:68
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Definition: validatorin.cpp:53
Base class for all validator rules.
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.
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 ...
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
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