Cutelyst  2.13.0
validatorcharnotallowed.cpp
1 /*
2  * Copyright (C) 2019 Matthias Fehring <mf@huessenbergnetz.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "validatorcharnotallowed_p.h"
20 
21 using namespace Cutelyst;
22 
23 ValidatorCharNotAllowed::ValidatorCharNotAllowed(const QString &field, const QString &forbiddenChars, const ValidatorMessages &messages, const QString &defValKey) :
24  ValidatorRule(*new ValidatorCharNotAllowedPrivate(field, forbiddenChars, messages, defValKey))
25 {
26 
27 }
28 
30 {
31 
32 }
33 
34 bool ValidatorCharNotAllowed::validate(const QString &value, const QString &forbiddenChars, QChar *foundChar)
35 {
36  bool valid = true;
37 
38  for (const QChar &forbiddenChar : forbiddenChars) {
39  if (value.contains(forbiddenChar)) {
40  valid = false;
41  if (foundChar) {
42  *foundChar = forbiddenChar;
43  }
44  break;
45  }
46  }
47 
48  return valid;
49 }
50 
52 {
53  ValidatorReturnType result;
54 
55  Q_D(const ValidatorCharNotAllowed);
56 
57  const QString v = value(params);
58  if (!v.isEmpty()) {
59  if (Q_LIKELY(!d->forbiddenChars.isEmpty())) {
60  QChar foundChar;
61  if (Q_LIKELY(ValidatorCharNotAllowed::validate(v, d->forbiddenChars, &foundChar))) {
62  result.value.setValue<QString>(v);
63  } else {
64  result.errorMessage = validationError(c, foundChar);
65  }
66  } else {
67  qCWarning(C_VALIDATOR) << "ValidatorCharNotAllowed: Empty validation data for field" << field() << "at" << c->controllerName() << "::" << c->actionName();
69  }
70  } else {
71  defaultValue(c, &result, "ValidatorCharNotAllowed");
72  }
73 
74  return result;
75 }
76 
77 QString ValidatorCharNotAllowed::genericValidationError(Context *c, const QVariant &errorData) const
78 {
79  QString error;
80  const QChar foundChar = errorData.toChar();
81  Q_D(const ValidatorCharNotAllowed);
82  const QString _label = label(c);
83  if (_label.isEmpty()) {
84  error = c->translate("Cutelyst::ValidatorCharNotAllowed", "Must not contain the following characters: “%1”. But contains the following illegal character: “%2”.").arg(d->forbiddenChars, QString(foundChar));
85  } else {
86  error = c->translate("Cutelyst::ValidatorCharNotAllowed", "The text in the “%1“ field must not contain the following characters: “%2“. But contains the following illegal character: “%3”.").arg(_label, d->forbiddenChars, QString(foundChar));
87  }
88 
89  return error;
90 }
91 
92 QString ValidatorCharNotAllowed::genericValidationDataError(Context *c, const QVariant &errorData) const
93 {
94  QString error;
95  Q_UNUSED(errorData)
96  const QString _label = label(c);
97  if (_label.isEmpty()) {
98  error = c->translate("Cutelyst::ValidatorCharNotAllowed", "The list of illegal characters for this field is empty.");
99  } else {
100  error = c->translate("Cutelyst::ValidatorCharNotAllowed", "The list of illegal characters for the “%1“ field is empty.").arg(_label);
101  }
102  return error;
103 }
Cutelyst::ParamsMultiMap
QMap< QString, QString > ParamsMultiMap
Definition: paramsmultimap.h:36
Cutelyst::ValidatorMessages
Stores custom error messages and the input field label.
Definition: validatorrule.h:144
Cutelyst::ValidatorRule::value
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
Definition: validatorrule.cpp:41
Cutelyst::ValidatorCharNotAllowed::~ValidatorCharNotAllowed
~ValidatorCharNotAllowed() override
Deconstructs the char not allowed validator.
Definition: validatorcharnotallowed.cpp:29
Cutelyst::ValidatorRule::label
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
Definition: validatorrule.cpp:58
Cutelyst::ValidatorRule::validationError
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Definition: validatorrule.cpp:72
Cutelyst::ValidatorCharNotAllowed::genericValidationDataError
QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if the list of forbidden characters is empty.
Definition: validatorcharnotallowed.cpp:92
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:50
Cutelyst::ValidatorCharNotAllowed::ValidatorCharNotAllowed
ValidatorCharNotAllowed(const QString &field, const QString &forbiddenChars, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new char not allowed validator.
Definition: validatorcharnotallowed.cpp:23
Cutelyst::ValidatorRule
Base class for all validator rules.
Definition: validatorrule.h:292
Cutelyst::ValidatorCharNotAllowed
Validates an input field for not allowed characters.
Definition: validatorcharnotallowed.h:47
Cutelyst::ValidatorCharNotAllowed::validate
static bool validate(const QString &value, const QString &forbiddenChars, QChar *foundChar=nullptr)
Returns true if value does not contain any of the charachters in forbiddenChars.
Definition: validatorcharnotallowed.cpp:34
Cutelyst::ValidatorRule::field
QString field() const
Returns the name of the field to validate.
Definition: validatorrule.cpp:39
Cutelyst::Context::translate
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:473
Cutelyst::ValidatorReturnType
Contains the result of a single input parameter validation.
Definition: validatorrule.h:62
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::ValidatorReturnType::value
QVariant value
Definition: validatorrule.h:64
Cutelyst::ValidatorReturnType::errorMessage
QString errorMessage
Definition: validatorrule.h:63
Cutelyst::ValidatorCharNotAllowed::genericValidationError
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Definition: validatorcharnotallowed.cpp:77
Cutelyst::ValidatorRule::validationDataError
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
Definition: validatorrule.cpp:132
Cutelyst::ValidatorRule::defaultValue
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 ...
Definition: validatorrule.cpp:162