cutelyst 4.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorcharnotallowed.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2019-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorcharnotallowed_p.h"
7
8using namespace Cutelyst;
9
11 const QString &forbiddenChars,
12 const ValidatorMessages &messages,
13 const QString &defValKey)
14 : ValidatorRule(*new ValidatorCharNotAllowedPrivate(field, forbiddenChars, messages, defValKey))
15{
16}
17
19
20bool ValidatorCharNotAllowed::validate(const QString &value,
21 const QString &forbiddenChars,
22 QChar *foundChar)
23{
24 bool valid = true;
25
26 for (const QChar &forbiddenChar : forbiddenChars) {
27 if (value.contains(forbiddenChar)) {
28 valid = false;
29 if (foundChar) {
30 *foundChar = forbiddenChar;
31 }
32 break;
33 }
34 }
35
36 return valid;
37}
38
40 const ParamsMultiMap &params) const
41{
43
44 Q_D(const ValidatorCharNotAllowed);
45
46 const QString v = value(params);
47 if (!v.isEmpty()) {
48 if (Q_LIKELY(!d->forbiddenChars.isEmpty())) {
49 QChar foundChar;
50 if (Q_LIKELY(ValidatorCharNotAllowed::validate(v, d->forbiddenChars, &foundChar))) {
51 result.value.setValue(v);
52 } else {
53 result.errorMessage = validationError(c, foundChar);
54 }
55 } else {
56 qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Empty validation data";
58 }
59 } else {
60 defaultValue(c, &result);
61 }
62
63 return result;
64}
65
66QString ValidatorCharNotAllowed::genericValidationError(Context *c, const QVariant &errorData) const
67{
68 QString error;
69 const QChar foundChar = errorData.toChar();
70 Q_D(const ValidatorCharNotAllowed);
71 const QString _label = label(c);
72 if (_label.isEmpty()) {
73 error = c->translate("Cutelyst::ValidatorCharNotAllowed",
74 "Must not contain the following characters: “%1”. But contains the "
75 "following illegal character: “%2”.")
76 .arg(d->forbiddenChars, QString(foundChar));
77 } else {
78 error =
79 c->translate("Cutelyst::ValidatorCharNotAllowed",
80 "The text in the “%1“ field must not contain the following characters: "
81 "“%2“. But contains the following illegal character: “%3”.")
82 .arg(_label, d->forbiddenChars, QString(foundChar));
83 }
84
85 return error;
86}
87
89 const QVariant &errorData) const
90{
91 QString error;
92 Q_UNUSED(errorData)
93 const QString _label = label(c);
94 if (_label.isEmpty()) {
95 error = c->translate("Cutelyst::ValidatorCharNotAllowed",
96 "The list of illegal characters for this field is empty.");
97 } else {
98 error = c->translate("Cutelyst::ValidatorCharNotAllowed",
99 "The list of illegal characters for the “%1“ field is empty.")
100 .arg(_label);
101 }
102 return error;
103}
The Cutelyst Context.
Definition: context.h:38
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
<Cutelyst/Plugins/Utils/validatorcharnotallowed.h>
QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if the list of forbidden characters is empty.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
~ValidatorCharNotAllowed() override
Deconstructs the char not allowed validator.
ValidatorCharNotAllowed(const QString &field, const QString &forbiddenChars, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new char not allowed validator.
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.
static bool validate(const QString &value, const QString &forbiddenChars, QChar *foundChar=nullptr)
Returns true if value does not contain any of the characters in forbiddenChars.
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