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