cutelyst  4.9.0
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-2023 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 
45 bool ValidatorAlphaDash::validate(const QString &value, bool asciiOnly)
46 {
47  bool valid = true;
48  if (asciiOnly) {
49  for (const QChar &ch : value) {
50  const ushort &uc = ch.unicode();
51  if (!(((uc >= ValidatorRulePrivate::ascii_A) &&
52  (uc <= ValidatorRulePrivate::ascii_Z)) ||
53  ((uc >= ValidatorRulePrivate::ascii_a) &&
54  (uc <= ValidatorRulePrivate::ascii_z)) ||
55  ((uc >= ValidatorRulePrivate::ascii_0) &&
56  (uc <= ValidatorRulePrivate::ascii_9)) ||
57  (uc == ValidatorRulePrivate::ascii_dash) ||
58  (uc == ValidatorRulePrivate::ascii_underscore))) {
59  valid = false;
60  break;
61  }
62  }
63  } else {
64  valid = value.contains(ValidatorAlphaDashPrivate::regex);
65  }
66  return valid;
67 }
68 
70  const QVariant &errorData) const
71 {
72  Q_UNUSED(errorData)
73  Q_D(const ValidatorAlphaDash);
74  const QString _label = label(c);
75  if (_label.isEmpty()) {
76  if (d->asciiOnly) {
77  //% "Must only contain alpha-numeric latin characters, dashes and underscores "
78  //% "from the ASCII character encoding (a-z, A-Z, 0-9, _ and -)."
79  return c->qtTrId("cutelyst-valalphadash-genvalerr-asciionly");
80  } else {
81  //% "Must only contain alpha-numeric characters, dashes and underscores."
82  return c->qtTrId("cutelyst-valalphadash-genvalerr");
83  }
84  } else {
85  if (d->asciiOnly) {
86  //: %1 will be replaced by the field label
87  //% "The text in the “%1” field must only contain alpha-numeric latin "
88  //% "characters, dashes and underscores from the ASCII character encondig "
89  //% "(a-z, A-Z, 0-9, _ and -)."
90  return c->qtTrId("cutelyst-valalphadash-genvalerr-asciionly-label").arg(_label);
91  } else {
92  //: %1 will be replaced by the field label
93  //% "The text in the “%1” field must only contain alpha-numeric "
94  //% "characters, dashes and underscores."
95  return c->qtTrId("cutelyst-valalphadash-genvalerr-label").arg(_label);
96  }
97  }
98 }
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.
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
ValidatorAlphaDash(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Checks a value for only alpha-numeric content and dashes and underscores.
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 arg(Args &&... args) const const
QString debugString(Context *c) const
void setValue(QVariant &&value)