cutelyst  5.0.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatoralphadash.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2025 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatoralphadash_p.h"
7 
8 using namespace Cutelyst;
9 using namespace Qt::Literals::StringLiterals;
10 
11 const QRegularExpression ValidatorAlphaDashPrivate::regex{u"^[\\pL\\pM\\pN_-]+$"_s};
12 
14  bool asciiOnly,
15  const ValidatorMessages &messages,
16  const QString &defValKey)
17  : ValidatorRule(*new ValidatorAlphaDashPrivate(field, asciiOnly, messages, defValKey))
18 {
19 }
20 
22 
24 {
25  ValidatorReturnType result;
26 
27  Q_D(const ValidatorAlphaDash);
28 
29  const QString v = value(params);
30  if (!v.isEmpty()) {
31  if (Q_LIKELY(ValidatorAlphaDash::validate(v, d->asciiOnly))) {
32  result.value.setValue(v);
33  } else {
34  qCDebug(C_VALIDATOR).noquote().nospace()
35  << debugString(c) << " \"" << v << "\" contains character that are not allowed";
36  result.errorMessage = validationError(c);
37  }
38  } else {
39  defaultValue(c, &result);
40  }
41 
42  return result;
43 }
44 
46  const ParamsMultiMap &params,
47  ValidatorRtFn cb) const
48 {
49  cb(validate(c, params));
50 }
51 
52 bool ValidatorAlphaDash::validate(const QString &value, bool asciiOnly)
53 {
54  bool valid = true;
55  if (asciiOnly) {
56  for (const QChar &ch : value) {
57  const ushort &uc = ch.unicode();
58  if (!(((uc >= ValidatorRulePrivate::ascii_A) &&
59  (uc <= ValidatorRulePrivate::ascii_Z)) ||
60  ((uc >= ValidatorRulePrivate::ascii_a) &&
61  (uc <= ValidatorRulePrivate::ascii_z)) ||
62  ((uc >= ValidatorRulePrivate::ascii_0) &&
63  (uc <= ValidatorRulePrivate::ascii_9)) ||
64  (uc == ValidatorRulePrivate::ascii_dash) ||
65  (uc == ValidatorRulePrivate::ascii_underscore))) {
66  valid = false;
67  break;
68  }
69  }
70  } else {
71  valid = value.contains(ValidatorAlphaDashPrivate::regex);
72  }
73  return valid;
74 }
75 
77  const QVariant &errorData) const
78 {
79  Q_UNUSED(errorData)
80  Q_D(const ValidatorAlphaDash);
81  const QString _label = label(c);
82  if (_label.isEmpty()) {
83  if (d->asciiOnly) {
84  //% "Must only contain alpha-numeric latin characters, dashes and underscores "
85  //% "from the ASCII character encoding (a-z, A-Z, 0-9, _ and -)."
86  return c->qtTrId("cutelyst-valalphadash-genvalerr-asciionly");
87  } else {
88  //% "Must only contain alpha-numeric characters, dashes and underscores."
89  return c->qtTrId("cutelyst-valalphadash-genvalerr");
90  }
91  } else {
92  if (d->asciiOnly) {
93  //: %1 will be replaced by the field label
94  //% "The text in the “%1” field must only contain alpha-numeric latin "
95  //% "characters, dashes and underscores from the ASCII character encondig "
96  //% "(a-z, A-Z, 0-9, _ and -)."
97  return c->qtTrId("cutelyst-valalphadash-genvalerr-asciionly-label").arg(_label);
98  } else {
99  //: %1 will be replaced by the field label
100  //% "The text in the “%1” field must only contain alpha-numeric "
101  //% "characters, dashes and underscores."
102  return c->qtTrId("cutelyst-valalphadash-genvalerr-label").arg(_label);
103  }
104  }
105 }
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
Stores custom error messages and the input field label.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
static bool validate(const QString &value, bool asciiOnly=false)
Returns true if the value only contains alpha-numeric characters, dashes and underscores.
The Cutelyst Context.
Definition: context.h:42
void defaultValue(Context *c, ValidatorReturnType *result) const
bool isEmpty() const const
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
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
Checks a value for only alpha-numeric content and dashes and underscores.
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:658
Contains the result of a single input parameter validation.
Definition: validatorrule.h:52
QString arg(Args &&... args) const const
ValidatorAlphaDash(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages={}, const QString &defValKey={})
void setValue(QVariant &&value)