cutelyst 3.9.1
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-2022 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
21
23 const QString &forbiddenChars,
24 QChar *foundChar)
25{
26 bool valid = true;
27
28 for (const QChar &forbiddenChar : forbiddenChars) {
29 if (value.contains(forbiddenChar)) {
30 valid = false;
31 if (foundChar) {
32 *foundChar = forbiddenChar;
33 }
34 break;
35 }
36 }
37
38 return valid;
39}
40
42 const ParamsMultiMap &params) const
43{
45
46 Q_D(const ValidatorCharNotAllowed);
47
48 const QString v = value(params);
49 if (!v.isEmpty()) {
50 if (Q_LIKELY(!d->forbiddenChars.isEmpty())) {
51 QChar foundChar;
52 if (Q_LIKELY(ValidatorCharNotAllowed::validate(v, d->forbiddenChars, &foundChar))) {
53 result.value.setValue(v);
54 } else {
55 result.errorMessage = validationError(c, foundChar);
56 }
57 } else {
58 qCWarning(C_VALIDATOR)
59 << "ValidatorCharNotAllowed: Empty validation data for field" << field() << "at"
60 << c->controllerName() << "::" << c->actionName();
62 }
63 } else {
64 defaultValue(c, &result, "ValidatorCharNotAllowed");
65 }
66
67 return result;
68}
69
71{
72 QString error;
73 const QChar foundChar = errorData.toChar();
74 Q_D(const ValidatorCharNotAllowed);
75 const QString _label = label(c);
76 if (_label.isEmpty()) {
77 error = c->translate("Cutelyst::ValidatorCharNotAllowed",
78 "Must not contain the following characters: “%1”. But contains the "
79 "following illegal character: “%2”.")
80 .arg(d->forbiddenChars, QString(foundChar));
81 } else {
82 error =
83 c->translate("Cutelyst::ValidatorCharNotAllowed",
84 "The text in the “%1“ field must not contain the following characters: "
85 "“%2“. But contains the following illegal character: “%3”.")
86 .arg(_label, d->forbiddenChars, QString(foundChar));
87 }
88
89 return error;
90}
91
93 const QVariant &errorData) const
94{
95 QString error;
96 Q_UNUSED(errorData)
97 const QString _label = label(c);
98 if (_label.isEmpty()) {
99 error = c->translate("Cutelyst::ValidatorCharNotAllowed",
100 "The list of illegal characters for this field is empty.");
101 } else {
102 error = c->translate("Cutelyst::ValidatorCharNotAllowed",
103 "The list of illegal characters for the “%1“ field is empty.")
104 .arg(_label);
105 }
106 return error;
107}
The Cutelyst Context.
Definition context.h:39
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition context.cpp:490
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.
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.
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
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
void setValue(const T &value)
QChar toChar() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.