cutelyst 3.9.1
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-2022 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
22
24{
26
27 Q_D(const ValidatorIn);
28
29 const QString v = value(params);
30 if (!v.isEmpty()) {
31 QStringList vals;
32
33 if (d->values.userType() == QMetaType::QStringList) {
34 vals = d->values.toStringList();
35 } else if (d->values.userType() == QMetaType::QString) {
36 vals = c->stash(d->values.toString()).toStringList();
37 }
38
39 if (vals.empty()) {
40 qCWarning(
41 C_VALIDATOR,
42 "ValidatorIn: The list of comparison values for the field %s at %s::%s is empty.",
43 qPrintable(field()),
44 qPrintable(c->controllerName()),
45 qPrintable(c->actionName()));
47 } else {
48 if (vals.contains(v, d->cs)) {
49 result.value.setValue(v);
50 } else {
51 qCDebug(C_VALIDATOR,
52 "ValidatorIn: Validation failed for field %s at %s::%s: \"%s\" is not part "
53 "of the list of comparison values.",
54 qPrintable(field()),
55 qPrintable(c->controllerName()),
56 qPrintable(c->actionName()),
57 qPrintable(v));
58 result.errorMessage = validationError(c, vals);
59 }
60 }
61 } else {
62 defaultValue(c, &result, "ValidatorIn");
63 }
64
65 return result;
66}
67
69{
70 QString error;
71 const QStringList vals = errorData.toStringList();
72 const QString _label = label(c);
73 if (_label.isEmpty()) {
74 //: %1 will be replaced by a comma separated list of allowed values
75 error = c->translate("Cutelyst::ValidatorIn", "Has to be one of the following values: %1")
76 .arg(c->locale().createSeparatedList(vals));
77 } else {
78 //: %1 will be replaced by the field label, %2 will be replaced by a comma separated list of
79 //: allowed values
80 error =
81 c->translate("Cutelyst::ValidatorIn",
82 "The value in the “%1” field has to be one of the following values: %2")
83 .arg(_label, c->locale().createSeparatedList(vals));
84 }
85 return error;
86}
87
89{
90 QString error;
91 Q_UNUSED(errorData)
92 const QString _label = label(c);
93 if (_label.isEmpty()) {
94 error = c->translate("Cutelyst::ValidatorIn", "The list of comparison values is empty.");
95 } else {
96 //: %1 will be replaced by the field label
97 error = c->translate("Cutelyst::ValidatorIn",
98 "The list of comparison values for the “%1” field is empty.")
99 .arg(_label);
100 }
101 return error;
102}
The Cutelyst Context.
Definition context.h:39
void stash(const QVariantHash &unite)
Definition context.cpp:566
QLocale locale() const noexcept
Definition context.cpp:466
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition context.cpp:490
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
~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.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error messages if the list of comparison values is empty.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString field() const
Returns the name of the field to validate.
void defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
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.
The Cutelyst namespace holds all public Cutelyst API.
Definition Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
bool empty() const const
QString createSeparatedList(const QStringList &list) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
CaseSensitivity
void setValue(const T &value)
QStringList toStringList() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.