cutelyst  5.0.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-2025 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 
20 std::optional<QChar> ValidatorCharNotAllowed::validate(const QString &value,
21  const QString &forbiddenChars)
22 {
23  auto it = std::ranges::find_if(forbiddenChars, [&value](const QChar &forbiddenChar) {
24  return value.contains(forbiddenChar);
25  });
26 
27  if (it != forbiddenChars.end()) {
28  return *it;
29  }
30 
31  return {};
32 }
33 
35  const ParamsMultiMap &params) const
36 {
37  ValidatorReturnType result;
38 
39  Q_D(const ValidatorCharNotAllowed);
40 
41  const QString v = value(params);
42  if (!v.isEmpty()) {
43  if (Q_LIKELY(!d->forbiddenChars.isEmpty())) {
44  auto foundChar = ValidatorCharNotAllowed::validate(v, d->forbiddenChars);
45  if (Q_LIKELY(!foundChar.has_value())) {
46  result.value.setValue(v);
47  } else {
48  result.errorMessage = validationError(c, foundChar.value());
49  }
50  } else {
51  qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Empty validation data";
53  }
54  } else {
55  defaultValue(c, &result);
56  }
57 
58  return result;
59 }
60 
62  const ParamsMultiMap &params,
63  ValidatorRtFn cb) const
64 {
65  cb(validate(c, params));
66 }
67 
69 {
70  const QChar foundChar = errorData.toChar();
71  Q_D(const ValidatorCharNotAllowed);
72  const QString _label = label(c);
73  if (_label.isEmpty()) {
74  //: %1 will be replaced by string of forbidden chars, %2 by the forbidden
75  //: char that has been found in the input
76  //% "Must not contain the following characters: “%1”. But it contains the "
77  //% "this illegal character: “%2”."
78  return c->qtTrId("cutelyst-valcharnotallowed-genvalerr")
79  .arg(d->forbiddenChars, QString(foundChar));
80  } else {
81  //: %1 will be replaced by the field label, %2 by the list of forbidden chars
82  //: as a string, %3 will be the forbidden char that has been found
83  //% "The text in the “%1“ field must not contain the following characters: "
84  //% "“%2“. But it contains this illegal character: “%3”."
85  return c->qtTrId("cutelyst-valcharnotallowed-genvalerr-label")
86  .arg(_label, d->forbiddenChars, QString(foundChar));
87  }
88 }
89 
91  const QVariant &errorData) const
92 {
93  Q_UNUSED(errorData)
94  const QString _label = label(c);
95  if (_label.isEmpty()) {
96  //% "The list of illegal characters for this field is empty."
97  return c->qtTrId("cutelyst-valcharnotallowed-genvaldataerr");
98  } else {
99  //% "The list of illegal characters for the “%1“ field is empty."
100  return c->qtTrId("cutelyst-valcharnotallowed-genvaldataerr-label").arg(_label);
101  }
102 }
iterator end()
Stores custom error messages and the input field label.
QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const override
The Cutelyst Context.
Definition: context.h:42
void defaultValue(Context *c, ValidatorReturnType *result) const
bool isEmpty() const const
static std::optional< QChar > validate(const QString &value, const QString &forbiddenChars)
Returns true if value does not contain any of the forbideden characters.
The Cutelyst namespace holds all public Cutelyst API.
QString debugString(const Context *c) const
Base class for all validator rules.
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
Validates an input field for not allowed characters.
ValidatorCharNotAllowed(const QString &field, const QString &forbiddenChars, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey={})
QString value(const ParamsMultiMap &params) const
QString label(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
Definition: validatorrule.h:82
QString validationError(Context *c, const QVariant &errorData={}) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:658
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
Contains the result of a single input parameter validation.
Definition: validatorrule.h:52
QString arg(Args &&... args) const const
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
QChar toChar() const const
void setValue(QVariant &&value)