Cutelyst  2.14.2
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 
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 
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 }
~ValidatorCharNotAllowed() override
Deconstructs the char not allowed validator.
QString field() const
Returns the name of the field to validate.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
Stores custom error messages and the input field label.
ValidatorCharNotAllowed(const QString &field, const QString &forbiddenChars, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new char not allowed validator.
QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if the list of forbidden characters is empty.
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.
The Cutelyst Context.
Definition: context.h:51
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
bool isEmpty() const
Base class for all validator rules.
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 ...
bool contains(QChar ch, Qt::CaseSensitivity cs) const
Validates an input field for not allowed characters.
void setValue(const T &value)
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:481
Contains the result of a single input parameter validation.
Definition: validatorrule.h:62
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
QChar toChar() const