cutelyst  3.9.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-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatoralpha_p.h"
7 
8 #include <QRegularExpression>
9 
10 using namespace Cutelyst;
11 
13  bool asciiOnly,
14  const Cutelyst::ValidatorMessages &messages,
15  const QString &defValKey)
16  : ValidatorRule(*new ValidatorAlphaPrivate(field, asciiOnly, messages, defValKey))
17 {
18 }
19 
21 {
22 }
23 
25  const ParamsMultiMap &params) const
26 {
27  ValidatorReturnType result;
28 
29  Q_D(const ValidatorAlpha);
30 
31  const QString v = value(params);
32  if (!v.isEmpty()) {
33  if (Q_LIKELY(ValidatorAlpha::validate(v, d->asciiOnly))) {
34  result.value.setValue(v);
35  } else {
36  qCDebug(C_VALIDATOR,
37  "ValidatorAlhpa: Validation failed for field %s at %s::%s: %s contains "
38  "characters that are not allowed.",
39  qPrintable(field()),
40  qPrintable(c->controllerName()),
41  qPrintable(c->actionName()),
42  qPrintable(v));
43  result.errorMessage = validationError(c);
44  }
45  } else {
46  defaultValue(c, &result, "ValidatorAlpha");
47  }
48 
49  return result;
50 }
51 
52 bool ValidatorAlpha::validate(const QString &value, bool asciiOnly)
53 {
54  bool valid = true;
55 
56  if (asciiOnly) {
57  for (const QChar &ch : value) {
58  const ushort &uc = ch.unicode();
59  if (!(((uc > 64) && (uc < 91)) || ((uc > 96) && (uc < 123)))) {
60  valid = false;
61  break;
62  }
63  }
64  } else {
65  valid = value.contains(QRegularExpression(QStringLiteral("^[\\pL\\pM]+$")));
66  }
67 
68  return valid;
69 }
70 
72 {
73  QString error;
74  Q_UNUSED(errorData)
75  Q_D(const ValidatorAlpha);
76  const QString _label = label(c);
77  if (_label.isEmpty()) {
78  if (d->asciiOnly) {
79  error = c->translate("Cutelyst::ValidatorAlhpa",
80  "Must only contain alphabetical latin characters.");
81  } else {
82  error = c->translate("Cutelyst::ValidatorAlhpa",
83  "Must only contain alphabetical characters.");
84  }
85  } else {
86  if (d->asciiOnly) {
87  //: %1 will be replaced by the field label
88  error =
89  c->translate(
90  "Cutelyst::ValidatorAlhpa",
91  "The text in the “%1” field must only contain alphabetical latin characters.")
92  .arg(_label);
93  } else {
94  //: %1 will be replaced by the field label
95  error = c->translate(
96  "Cutelyst::ValidatorAlhpa",
97  "The text in the “%1” field must only contain alphabetical characters.")
98  .arg(_label);
99  }
100  }
101  return error;
102 }
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
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())
Constructs a new alpha validator.
Stores custom error messages and the input field label.
Validates an input field for only alphabetic content.
The Cutelyst Context.
Definition: context.h:38
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:490
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
~ValidatorAlpha() override
Deconstructs the alpha validator.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
void defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...