cutelyst  3.7.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatornotin.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatornotin_p.h"
7 
8 using namespace Cutelyst;
9 
10 ValidatorNotIn::ValidatorNotIn(const QString &field, const QStringList &values, Qt::CaseSensitivity cs, const Cutelyst::ValidatorMessages &messages, const QString &defValKey) :
11  ValidatorRule(*new ValidatorNotInPrivate(field, values, cs, messages, defValKey))
12 {
13 }
14 
16 {
17 }
18 
20 {
21  ValidatorReturnType result;
22 
23  Q_D(const ValidatorNotIn);
24 
25  if (d->values.empty()) {
27  qCWarning(C_VALIDATOR, "ValidatorNotIn: The list of comparison values for the field %s at %s::%s is empty.", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
28  } else {
29  const QString v = value(params);
30  if (!v.isEmpty()) {
31  if (d->values.contains(v, d->cs)) {
32  result.errorMessage = validationError(c);
33  qCDebug(C_VALIDATOR, "ValidatorNotIn: Validation failed for field %s at %s::%s: \"%s\" is part of the list of not allowed comparison values.", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), qPrintable(v));
34  } else {
35  result.value.setValue(v);
36  }
37  } else {
38  defaultValue(c, &result, "ValidatorNotIn");
39  }
40  }
41 
42  return result;
43 }
44 
45 QString ValidatorNotIn::genericValidationError(Context *c, const QVariant &errorData) const
46 {
47  QString error;
48  Q_UNUSED(errorData)
49  const QString _label = label(c);
50  if (_label.isEmpty()) {
51  error = c->translate("Cutelyst::ValidatorNotIn", "Value is not allowed.");
52  } else {
53  error = c->translate("Cutelyst::ValidatorNotIn", "The value in the “%1” field is not allowed.").arg(_label);
54  }
55  return error;
56 }
57 
58 QString ValidatorNotIn::genericValidationDataError(Context *c, const QVariant &errorData) const
59 {
60  QString error;
61  Q_UNUSED(errorData)
62  const QString _label = label(c);
63  if (_label.isEmpty()) {
64  error = c->translate("Cutelyst::ValidatorNotIn", "The list of comparison values is empty.");
65  } else {
66  error = c->translate("Cutelyst::ValidatorNotIn", "The list of comparison values for the “%1” field is empty.").arg(_label);
67  }
68  return error;
69 }
The Cutelyst Context.
Definition: context.h:39
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 not one from a list of values.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
~ValidatorNotIn() override
Deconstructs the validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error messages if the list of comparison values is empty.
ValidatorNotIn(const QString &field, const QStringList &values, Qt::CaseSensitivity cs=Qt::CaseSensitive, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new not in validator.
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