cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrequiredunlessstash.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2018-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorrequiredunlessstash_p.h"
7
8using namespace Cutelyst;
9
11 const QString &stashKey,
12 const QVariant &stashValues,
13 const ValidatorMessages &messages)
15 *new ValidatorRequiredUnlessStashPrivate(field, stashKey, stashValues, messages))
16{
17}
18
20
22 const ParamsMultiMap &params) const
23{
25
27
28 if (d->stashKey.isEmpty() || d->stashValues.isNull() || !d->stashValues.isValid()) {
30 qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid validation data";
31 return result;
32 }
33
34 const QString v = value(params);
35 const QVariant sv = c->stash(d->stashKey);
36 bool isRequired = false;
37
38 if (d->stashValues.typeId() == QMetaType::QString) {
39
40 const QVariant _stashValues = c->stash(d->stashValues.toString());
41
42 if (_stashValues.typeId() == QMetaType::QStringList) {
43
44 const auto str = sv.toString();
45 const auto strLst = _stashValues.toStringList();
46 isRequired = !strLst.contains(str);
47
48 } else if (_stashValues.typeId() == QMetaType::QVariantList) {
49
50 const auto varLst = _stashValues.toList();
51 isRequired = !varLst.contains(sv);
52
53 } else {
55 qCWarning(C_VALIDATOR).noquote().nospace()
56 << debugString(c) << "Invalid validation data. The stash key \""
57 << d->stashValues.toString()
58 << "\" does not contain a QStringList or a QVariantList.";
59 return result;
60 }
61
62 } else if (d->stashValues.typeId() == QMetaType::QStringList) {
63
64 const auto str = sv.toString();
65 const auto strLst = d->stashValues.toStringList();
66 isRequired = !strLst.contains(str);
67
68 } else if (d->stashValues.typeId() == QMetaType::QVariantList) {
69
70 const auto varList = d->stashValues.toList();
71 isRequired = !varList.contains(sv);
72
73 } else {
75 qCWarning(C_VALIDATOR).noquote() << debugString(c)
76 << "Invalid validation data. "
77 "stashValues has to be one of QString, "
78 "QStringList or QVariantList.";
79 return result;
80 }
81
82 if (isRequired && v.isEmpty()) {
83 result.errorMessage = validationError(c);
84 qCDebug(C_VALIDATOR).noquote().nospace()
85 << debugString(c) << " The field is not present or empty but stash key \""
86 << d->stashKey << "\" does not contain " << sv;
87 return result;
88 }
89
90 if (!v.isEmpty()) {
91 result.value.setValue(v);
92 }
93
94 return result;
95}
96
98 const QVariant &errorData) const
99{
100 // translation strings are defined in ValidatorRequired
101 Q_UNUSED(errorData)
102 const QString _label = label(c);
103 if (_label.isEmpty()) {
104 return c->qtTrId("cutelyst-validator-genvalerr-req");
105 } else {
106 return c->qtTrId("cutelyst-validator-genvalerr-req-label").arg(_label);
107 }
108}
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
The field under validation must be present and not empty unless the content of a stash key is equal t...
ValidatorRequiredUnlessStash(const QString &field, const QString &stashKey, const QVariant &stashValues, const ValidatorMessages &messages=ValidatorMessages())
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) 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
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
The Cutelyst namespace holds all public Cutelyst API.
QString arg(Args &&... args) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
void setValue(QVariant &&value)
QList< QVariant > toList() const const
QString toString() const const
QStringList toStringList() const const
int typeId() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.