cutelyst  4.5.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-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 
10 const QRegularExpression ValidatorAlphaDashPrivate::regex{u"^[\\pL\\pM\\pN_-]+$"_qs};
11 
13  bool asciiOnly,
14  const ValidatorMessages &messages,
15  const QString &defValKey)
16  : ValidatorRule(*new ValidatorAlphaDashPrivate(field, asciiOnly, messages, defValKey))
17 {
18 }
19 
21 
23 {
24  ValidatorReturnType result;
25 
26  Q_D(const ValidatorAlphaDash);
27 
28  const QString v = value(params);
29  if (!v.isEmpty()) {
30  if (Q_LIKELY(ValidatorAlphaDash::validate(v, d->asciiOnly))) {
31  result.value.setValue(v);
32  } else {
33  qCDebug(C_VALIDATOR).noquote().nospace()
34  << debugString(c) << " \"" << v << "\" contains character that are not allowed";
35  result.errorMessage = validationError(c);
36  }
37  } else {
38  defaultValue(c, &result);
39  }
40 
41  return result;
42 }
43 
44 bool ValidatorAlphaDash::validate(const QString &value, bool asciiOnly)
45 {
46  bool valid = true;
47  if (asciiOnly) {
48  for (const QChar &ch : value) {
49  const ushort &uc = ch.unicode();
50  if (!(((uc >= ValidatorRulePrivate::ascii_A) &&
51  (uc <= ValidatorRulePrivate::ascii_Z)) ||
52  ((uc >= ValidatorRulePrivate::ascii_a) &&
53  (uc <= ValidatorRulePrivate::ascii_z)) ||
54  ((uc >= ValidatorRulePrivate::ascii_0) &&
55  (uc <= ValidatorRulePrivate::ascii_9)) ||
56  (uc == ValidatorRulePrivate::ascii_dash) ||
57  (uc == ValidatorRulePrivate::ascii_underscore))) {
58  valid = false;
59  break;
60  }
61  }
62  } else {
63  valid = value.contains(ValidatorAlphaDashPrivate::regex);
64  }
65  return valid;
66 }
67 
69  const QVariant &errorData) const
70 {
71  Q_UNUSED(errorData)
72  Q_D(const ValidatorAlphaDash);
73  const QString _label = label(c);
74  if (_label.isEmpty()) {
75  if (d->asciiOnly) {
76  //% "Must only contain alpha-numeric latin characters, dashes and underscores "
77  //% "from the ASCII character encoding (a-z, A-Z, 0-9, _ and -)."
78  return c->qtTrId("cutelyst-valalphadash-genvalerr-asciionly");
79  } else {
80  //% "Must only contain alpha-numeric characters, dashes and underscores."
81  return c->qtTrId("cutelyst-valalphadash-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 "
87  //% "characters, dashes and underscores from the ASCII character encondig "
88  //% "(a-z, A-Z, 0-9, _ and -)."
89  return c->qtTrId("cutelyst-valalphadash-genvalerr-asciionly-label").arg(_label);
90  } else {
91  //: %1 will be replaced by the field label
92  //% "The text in the “%1” field must only contain alpha-numeric "
93  //% "characters, dashes and underscores."
94  return c->qtTrId("cutelyst-valalphadash-genvalerr-label").arg(_label);
95  }
96  }
97 }
The Cutelyst Context.
Definition: context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:656
Checks a value for only alpha-numeric content and dashes and underscores.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorAlphaDash(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
static bool validate(const QString &value, bool asciiOnly=false)
Returns true if the value only contains alpha-numeric characters, dashes and underscores.
The Cutelyst namespace holds all public Cutelyst API.
QString arg(Args &&... args) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
void setValue(QVariant &&value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49