cutelyst 4.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatornotin.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatornotin_p.h"
7
8using namespace Cutelyst;
9
10ValidatorNotIn::ValidatorNotIn(const QString &field,
11 const QVariant &values,
12 Qt::CaseSensitivity cs,
13 const Cutelyst::ValidatorMessages &messages,
14 const QString &defValKey)
15 : ValidatorRule(*new ValidatorNotInPrivate(field, values, cs, messages, defValKey))
16{
17}
18
20
22 const ParamsMultiMap &params) const
23{
25
26 Q_D(const ValidatorNotIn);
27
28 const QString v = value(params);
29 if (!v.isEmpty()) {
30 QStringList vals;
31
32 if (d->values.userType() == QMetaType::QStringList) {
33 vals = d->values.toStringList();
34 } else if (d->values.userType() == QMetaType::QString) {
35 vals = c->stash(d->values.toString()).toStringList();
36 }
37
38 if (vals.empty()) {
40 qCWarning(C_VALIDATOR).noquote()
41 << debugString(c) << "The list of comparison values is empty";
42 } else {
43 if (vals.contains(v, d->cs)) {
44 result.errorMessage = validationError(c);
45 qCDebug(C_VALIDATOR).noquote().nospace()
46 << debugString(c) << " \"" << v
47 << "\" is part of the list of not allowed values" << d->values;
48 } else {
49 result.value.setValue(v);
50 }
51 }
52 } else {
53 defaultValue(c, &result);
54 }
55
56 return result;
57}
58
59QString ValidatorNotIn::genericValidationError(Context *c, const QVariant &errorData) const
60{
61 QString error;
62 Q_UNUSED(errorData)
63 const QString _label = label(c);
64 if (_label.isEmpty()) {
65 error = c->translate("Cutelyst::ValidatorNotIn", "Value is not allowed.");
66 } else {
67 error =
68 c->translate("Cutelyst::ValidatorNotIn", "The value in the “%1” field is not allowed.")
69 .arg(_label);
70 }
71 return error;
72}
73
74QString ValidatorNotIn::genericValidationDataError(Context *c, const QVariant &errorData) const
75{
76 QString error;
77 Q_UNUSED(errorData)
78 const QString _label = label(c);
79 if (_label.isEmpty()) {
80 error = c->translate("Cutelyst::ValidatorNotIn", "The list of comparison values is empty.");
81 } else {
82 error = c->translate("Cutelyst::ValidatorNotIn",
83 "The list of comparison values for the “%1” field is empty.")
84 .arg(_label);
85 }
86 return error;
87}
The Cutelyst Context.
Definition: context.h:38
void stash(const QVariantHash &unite)
Definition: context.cpp:553
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 not one from a list of values.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
~ValidatorNotIn() override
Deconstructs the validator.
ValidatorNotIn(const QString &field, const QVariant &values, Qt::CaseSensitivity cs=Qt::CaseSensitive, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new not in validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error messages if the list of comparison values is empty.
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