cutelyst 4.0.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
10ValidatorIn::ValidatorIn(const QString &field,
11 const QVariant &values,
12 Qt::CaseSensitivity cs,
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
58QString ValidatorIn::genericValidationError(Context *c, const QVariant &errorData) const
59{
60 QString error;
61 const QStringList vals = errorData.toStringList();
62 const QString _label = label(c);
63 if (_label.isEmpty()) {
64 //: %1 will be replaced by a comma separated list of allowed values
65 error = c->translate("Cutelyst::ValidatorIn", "Has to be one of the following values: %1")
66 .arg(c->locale().createSeparatedList(vals));
67 } else {
68 //: %1 will be replaced by the field label, %2 will be replaced by a comma separated list of
69 //: allowed values
70 error =
71 c->translate("Cutelyst::ValidatorIn",
72 "The value in the “%1” field has to be one of the following values: %2")
73 .arg(_label, c->locale().createSeparatedList(vals));
74 }
75 return error;
76}
77
78QString ValidatorIn::genericValidationDataError(Context *c, const QVariant &errorData) const
79{
80 QString error;
81 Q_UNUSED(errorData)
82 const QString _label = label(c);
83 if (_label.isEmpty()) {
84 error = c->translate("Cutelyst::ValidatorIn", "The list of comparison values is empty.");
85 } else {
86 //: %1 will be replaced by the field label
87 error = c->translate("Cutelyst::ValidatorIn",
88 "The list of comparison values for the “%1” field is empty.")
89 .arg(_label);
90 }
91 return error;
92}
The Cutelyst Context.
Definition: context.h:38
void stash(const QVariantHash &unite)
Definition: context.cpp:553
QLocale locale() const noexcept
Definition: context.cpp:453
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
Checks if the field value is one from a list of values.
Definition: validatorin.h:35
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
Definition: validatorin.cpp:21
~ValidatorIn() override
Deconstructs the in validator.
ValidatorIn(const QString &field, const QVariant &values, Qt::CaseSensitivity cs=Qt::CaseSensitive, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new in validator.
Definition: validatorin.cpp:10
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error messages if the list of comparison values is empty.
Definition: validatorin.cpp:78
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Definition: validatorin.cpp:58
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
void defaultValue(Context *c, ValidatorReturnType *result) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49