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