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