cutelyst  3.9.1
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 
11  const QStringList &values,
13  const Cutelyst::ValidatorMessages &messages,
14  const QString &defValKey)
15  : ValidatorRule(*new ValidatorNotInPrivate(field, values, cs, messages, defValKey))
16 {
17 }
18 
20 {
21 }
22 
24  const ParamsMultiMap &params) const
25 {
26  ValidatorReturnType result;
27 
28  Q_D(const ValidatorNotIn);
29 
30  if (d->values.empty()) {
32  qCWarning(
33  C_VALIDATOR,
34  "ValidatorNotIn: The list of comparison values for the field %s at %s::%s is empty.",
35  qPrintable(field()),
36  qPrintable(c->controllerName()),
37  qPrintable(c->actionName()));
38  } else {
39  const QString v = value(params);
40  if (!v.isEmpty()) {
41  if (d->values.contains(v, d->cs)) {
42  result.errorMessage = validationError(c);
43  qCDebug(C_VALIDATOR,
44  "ValidatorNotIn: Validation failed for field %s at %s::%s: \"%s\" is part "
45  "of the list of not allowed comparison values.",
46  qPrintable(field()),
47  qPrintable(c->controllerName()),
48  qPrintable(c->actionName()),
49  qPrintable(v));
50  } else {
51  result.value.setValue(v);
52  }
53  } else {
54  defaultValue(c, &result, "ValidatorNotIn");
55  }
56  }
57 
58  return result;
59 }
60 
62 {
63  QString error;
64  Q_UNUSED(errorData)
65  const QString _label = label(c);
66  if (_label.isEmpty()) {
67  error = c->translate("Cutelyst::ValidatorNotIn", "Value is not allowed.");
68  } else {
69  error =
70  c->translate("Cutelyst::ValidatorNotIn", "The value in the “%1” field is not allowed.")
71  .arg(_label);
72  }
73  return error;
74 }
75 
77 {
78  QString error;
79  Q_UNUSED(errorData)
80  const QString _label = label(c);
81  if (_label.isEmpty()) {
82  error = c->translate("Cutelyst::ValidatorNotIn", "The list of comparison values is empty.");
83  } else {
84  error = c->translate("Cutelyst::ValidatorNotIn",
85  "The list of comparison values for the “%1” field is empty.")
86  .arg(_label);
87  }
88  return error;
89 }
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.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error messages if the list of comparison values is empty.
The Cutelyst Context.
Definition: context.h:38
CaseSensitivity
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:490
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 label(Context *c) const
Returns the human readable field label used for generic error messages.
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
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.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
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 ...
~ValidatorNotIn() override
Deconstructs the validator.
Checks if the field value is not one from a list of values.