cutelyst  5.0.1
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-2025 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 
47 {
48  cb(validate(c, params));
49 }
50 
51 bool ValidatorAlpha::validate(const QString &value, bool asciiOnly)
52 {
53  bool valid = true;
54 
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  valid = false;
63  break;
64  }
65  }
66  } else {
67  valid = value.contains(ValidatorAlphaPrivate::regex);
68  }
69 
70  return valid;
71 }
72 
74 {
75  Q_UNUSED(errorData)
76  Q_D(const ValidatorAlpha);
77  const QString _label = label(c);
78  if (_label.isEmpty()) {
79  if (d->asciiOnly) {
80  //% "Must only contain alphabetical characters from the ASCII character "
81  //% "encoding (a-z and A-Z)."
82  return c->qtTrId("cutelyst-valalpha-genvalerr-asciionly");
83  } else {
84  //% "Must only contain alphabetical characters."
85  return c->qtTrId("cutelyst-valalpha-genvalerr");
86  }
87  } else {
88  if (d->asciiOnly) {
89  //: %1 will be replaced by the field label
90  //% "The text in the “%1” field must only contain alphabetical characters "
91  //% "from the ASCII character encoding (a-z and A-Z)."
92  return c->qtTrId("cutelyst-valalpha-genvalerr-asciionly-label").arg(_label);
93  } else {
94  //: %1 will be replaced by the field label
95  //% "The text in the “%1” field must only contain alphabetical characters."
96  return c->qtTrId("cutelyst-valalpha-genvalerr-label").arg(_label);
97  }
98  }
99 }
static bool validate(const QString &value, bool asciiOnly=false)
Returns true if value only contains alphabetic characters.
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
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
ValidatorAlpha(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages={}, const QString &defValKey={})
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
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 genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
QString arg(Args &&... args) const const
void setValue(QVariant &&value)