cutelyst  4.5.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-2023 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 QVariant &values,
13  const Cutelyst::ValidatorMessages &messages,
14  const QString &defValKey)
15  : ValidatorRule(*new ValidatorNotInPrivate(field, values, cs, messages, defValKey))
16 {
17 }
18 
20 
22  const ParamsMultiMap &params) const
23 {
24  ValidatorReturnType result;
25 
26  Q_D(const ValidatorNotIn);
27 
28  const QString v = value(params);
29  if (!v.isEmpty()) {
30  QStringList vals;
31 
32  if (d->values.userType() == QMetaType::QStringList) {
33  vals = d->values.toStringList();
34  } else if (d->values.userType() == QMetaType::QString) {
35  vals = c->stash(d->values.toString()).toStringList();
36  }
37 
38  if (vals.empty()) {
40  qCWarning(C_VALIDATOR).noquote()
41  << debugString(c) << "The list of comparison values is empty";
42  } else {
43  if (vals.contains(v, d->cs)) {
44  result.errorMessage = validationError(c);
45  qCDebug(C_VALIDATOR).noquote().nospace()
46  << debugString(c) << " \"" << v
47  << "\" is part of the list of not allowed values" << d->values;
48  } else {
49  result.value.setValue(v);
50  }
51  }
52  } else {
53  defaultValue(c, &result);
54  }
55 
56  return result;
57 }
58 
60 {
61  Q_UNUSED(errorData)
62  const QString _label = label(c);
63  if (_label.isEmpty()) {
64  //% "Value is not allowed."
65  return c->qtTrId("cutelyst-valnotin-genvalerr");
66  } else {
67  //% "The value in the “%1” field is not allowed."
68  return c->qtTrId("cutelyst-valnotin-genvalerr-label").arg(_label);
69  }
70 }
71 
73 {
74  // translation strings are defined in ValidatorIn
75 
76  Q_UNUSED(errorData)
77  const QString _label = label(c);
78  if (_label.isEmpty()) {
79  return c->qtTrId("cutelyst-validator-genvaldataerr-empty-list");
80  } else {
81  return c->qtTrId("cutelyst-validator-genvaldataerr-empty-list-label").arg(_label);
82  }
83 }
The Cutelyst Context.
Definition: context.h:42
void stash(const QVariantHash &unite)
Definition: context.cpp:562
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:656
Checks if the field value is not one from a list of values.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
ValidatorNotIn(const QString &field, const QVariant &values, Qt::CaseSensitivity cs=Qt::CaseSensitive, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
The Cutelyst namespace holds all public Cutelyst API.
bool empty() const const
QString arg(Args &&... args) const const
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
CaseSensitivity
void setValue(QVariant &&value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49