cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorin.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorin_p.h"
7
8using namespace Cutelyst;
9
11 const QVariant &values,
13 const Cutelyst::ValidatorMessages &messages,
14 const QString &defValKey)
15 : ValidatorRule(*new ValidatorInPrivate(field, values, cs, messages, defValKey))
16{
17}
18
20
22{
24
25 Q_D(const ValidatorIn);
26
27 const QString v = value(params);
28 if (!v.isEmpty()) {
29 QStringList vals;
30
31 if (d->values.userType() == QMetaType::QStringList) {
32 vals = d->values.toStringList();
33 } else if (d->values.userType() == QMetaType::QString) {
34 vals = c->stash(d->values.toString()).toStringList();
35 }
36
37 if (vals.empty()) {
39 qCWarning(C_VALIDATOR).noquote()
40 << debugString(c) << "The list of comparison values is emtpy";
41 } else {
42 if (vals.contains(v, d->cs)) {
43 result.value.setValue(v);
44 } else {
45 qCDebug(C_VALIDATOR).noquote().nospace()
46 << debugString(c) << " \"" << v << "\" is not part of the comparison list "
47 << vals;
48 result.errorMessage = validationError(c, vals);
49 }
50 }
51 } else {
52 defaultValue(c, &result);
53 }
54
55 return result;
56}
57
59{
60 const QStringList vals = errorData.toStringList();
61 const QString _label = label(c);
62 if (_label.isEmpty()) {
63 //: %1 will be replaced by a comma separated list of allowed values
64 //% "Has to be one of the following values: %1"
65 return c->qtTrId("cutelyst-valin-genvalerr").arg(c->locale().createSeparatedList(vals));
66 } else {
67 //: %1 will be replaced by the field label, %2 will be replaced by a comma
68 //: separated list of allowed values
69 //% "The value in the “%1” field has to be one of the following values: %2"
70 return c->qtTrId("cutelyst-valin-genvalerr-label")
71 .arg(_label, c->locale().createSeparatedList(vals));
72 }
73}
74
76{
77 Q_UNUSED(errorData)
78 const QString _label = label(c);
79 if (_label.isEmpty()) {
80 //% "The list of comparison values is empty."
81 return c->qtTrId("cutelyst-validator-genvaldataerr-empty-list");
82 } else {
83 //: %1 will be replaced by the field label
84 //% "The list of comparison values for the “%1” field is empty."
85 return c->qtTrId("cutelyst-validator-genvaldataerr-empty-list-label").arg(_label);
86 }
87}
The Cutelyst Context.
Definition context.h:42
void stash(const QVariantHash &unite)
Definition context.cpp:562
QLocale locale() const noexcept
Definition context.cpp:460
QString qtTrId(const char *id, int n=-1) const
Definition context.h:656
Checks if the field value is one from a list of values.
Definition validatorin.h:38
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
ValidatorIn(const QString &field, const QVariant &values, Qt::CaseSensitivity cs=Qt::CaseSensitive, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) 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 createSeparatedList(const QStringList &list) 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)
QStringList toStringList() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.