Cutelyst  3.5.0
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 
8 using namespace Cutelyst;
9 
10 ValidatorCharNotAllowed::ValidatorCharNotAllowed(const QString &field, const QString &forbiddenChars, const ValidatorMessages &messages, const QString &defValKey) :
11  ValidatorRule(*new ValidatorCharNotAllowedPrivate(field, forbiddenChars, messages, defValKey))
12 {
13 
14 }
15 
17 {
18 
19 }
20 
21 bool ValidatorCharNotAllowed::validate(const QString &value, const QString &forbiddenChars, QChar *foundChar)
22 {
23  bool valid = true;
24 
25  for (const QChar &forbiddenChar : forbiddenChars) {
26  if (value.contains(forbiddenChar)) {
27  valid = false;
28  if (foundChar) {
29  *foundChar = forbiddenChar;
30  }
31  break;
32  }
33  }
34 
35  return valid;
36 }
37 
39 {
40  ValidatorReturnType result;
41 
42  Q_D(const ValidatorCharNotAllowed);
43 
44  const QString v = value(params);
45  if (!v.isEmpty()) {
46  if (Q_LIKELY(!d->forbiddenChars.isEmpty())) {
47  QChar foundChar;
48  if (Q_LIKELY(ValidatorCharNotAllowed::validate(v, d->forbiddenChars, &foundChar))) {
49  result.value.setValue(v);
50  } else {
51  result.errorMessage = validationError(c, foundChar);
52  }
53  } else {
54  qCWarning(C_VALIDATOR) << "ValidatorCharNotAllowed: Empty validation data for field" << field() << "at" << c->controllerName() << "::" << c->actionName();
56  }
57  } else {
58  defaultValue(c, &result, "ValidatorCharNotAllowed");
59  }
60 
61  return result;
62 }
63 
65 {
66  QString error;
67  const QChar foundChar = errorData.toChar();
68  Q_D(const ValidatorCharNotAllowed);
69  const QString _label = label(c);
70  if (_label.isEmpty()) {
71  error = c->translate("Cutelyst::ValidatorCharNotAllowed", "Must not contain the following characters: “%1”. But contains the following illegal character: “%2”.").arg(d->forbiddenChars, QString(foundChar));
72  } else {
73  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));
74  }
75 
76  return error;
77 }
78 
80 {
81  QString error;
82  Q_UNUSED(errorData)
83  const QString _label = label(c);
84  if (_label.isEmpty()) {
85  error = c->translate("Cutelyst::ValidatorCharNotAllowed", "The list of illegal characters for this field is empty.");
86  } else {
87  error = c->translate("Cutelyst::ValidatorCharNotAllowed", "The list of illegal characters for the “%1“ field is empty.").arg(_label);
88  }
89  return error;
90 }
~ValidatorCharNotAllowed() override
Deconstructs the char not allowed validator.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
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:38
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:471
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
Validates an input field for not allowed characters.
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
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 ...
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
QChar toChar() const const