cutelyst  4.4.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 
8 using 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 
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 {
42  ValidatorReturnType result;
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 
67 {
68  const QChar foundChar = errorData.toChar();
69  Q_D(const ValidatorCharNotAllowed);
70  const QString _label = label(c);
71  if (_label.isEmpty()) {
72  //: %1 will be replaced by string of forbidden chars, %2 by the forbidden
73  //: char that has been found in the input
74  //% "Must not contain the following characters: “%1”. But it contains the "
75  //% "this illegal character: “%2”."
76  return c->qtTrId("cutelyst-valcharnotallowed-genvalerr")
77  .arg(d->forbiddenChars, QString(foundChar));
78  } else {
79  //: %1 will be replaced by the field label, %2 by the list of forbidden chars
80  //: as a string, %3 will be the forbidden char that has been found
81  //% "The text in the “%1“ field must not contain the following characters: "
82  //% "“%2“. But it contains this illegal character: “%3”."
83  return c->qtTrId("cutelyst-valcharnotallowed-genvalerr-label")
84  .arg(_label, d->forbiddenChars, QString(foundChar));
85  }
86 }
87 
89  const QVariant &errorData) const
90 {
91  Q_UNUSED(errorData)
92  const QString _label = label(c);
93  if (_label.isEmpty()) {
94  //% "The list of illegal characters for this field is empty."
95  return c->qtTrId("cutelyst-valcharnotallowed-genvaldataerr");
96  } else {
97  //% "The list of illegal characters for the “%1“ field is empty."
98  return c->qtTrId("cutelyst-valcharnotallowed-genvaldataerr-label").arg(_label);
99  }
100 }
The Cutelyst Context.
Definition: context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:656
Validates an input field for not allowed characters.
QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const override
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorCharNotAllowed(const QString &field, const QString &forbiddenChars, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
static bool validate(const QString &value, const QString &forbiddenChars, QChar *foundChar=nullptr)
Returns true if value does not contain any of the forbideden characters.
The Cutelyst namespace holds all public Cutelyst API.
QString arg(Args &&... args) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
void setValue(QVariant &&value)
QChar toChar() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49