cutelyst 5.0.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-2025 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatornotin_p.h"
7
8using 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{
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 cb(validate(c, params));
62}
63
65{
66 Q_UNUSED(errorData)
67 const QString _label = label(c);
68 if (_label.isEmpty()) {
69 //% "Value is not allowed."
70 return c->qtTrId("cutelyst-valnotin-genvalerr");
71 } else {
72 //% "The value in the “%1” field is not allowed."
73 return c->qtTrId("cutelyst-valnotin-genvalerr-label").arg(_label);
74 }
75}
76
78{
79 // translation strings are defined in ValidatorIn
80
81 Q_UNUSED(errorData)
82 const QString _label = label(c);
83 if (_label.isEmpty()) {
84 return c->qtTrId("cutelyst-validator-genvaldataerr-empty-list");
85 } else {
86 return c->qtTrId("cutelyst-validator-genvaldataerr-empty-list-label").arg(_label);
87 }
88}
The Cutelyst Context.
Definition context.h:42
void stash(const QVariantHash &unite)
Definition context.cpp:563
QString qtTrId(const char *id, int n=-1) const
Definition context.h:657
Checks if the field value is not one from a list of values.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
ValidatorNotIn(const QString &field, const QVariant &values, Qt::CaseSensitivity cs=Qt::CaseSensitive, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey={})
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(const Context *c) const
QString debugString(const Context *c) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) 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.