cutelyst  3.7.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatoralphanum.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatoralphanum_p.h"
7 #include <QRegularExpression>
8 
9 using namespace Cutelyst;
10 
11 ValidatorAlphaNum::ValidatorAlphaNum(const QString &field, bool asciiOnly, const ValidatorMessages &messages, const QString &defValKey) :
12  ValidatorRule(*new ValidatorAlphaNumPrivate(field, asciiOnly, messages, defValKey))
13 {
14 
15 }
16 
18 {
19 
20 }
21 
23 {
24  ValidatorReturnType result;
25 
26  Q_D(const ValidatorAlphaNum);
27 
28  const QString v = value(params);
29  if (!v.isEmpty()) {
30  if (Q_LIKELY(ValidatorAlphaNum::validate(v, d->asciiOnly))) {
31  result.value.setValue(v);
32  } else {
33  qCDebug(C_VALIDATOR, "ValidatorAlphaNum: Validation failed for field %s at %s::%s: %s contains characters that are not allowed.", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), qPrintable(v));
34  result.errorMessage = validationError(c);
35  }
36 
37  } else {
38  defaultValue(c, &result, "ValidatorAlphaNum");
39  }
40 
41  return result;
42 }
43 
44 bool ValidatorAlphaNum::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 > 64) && (uc < 91)) || ((uc > 96) && (uc < 123)) || ((uc > 47) && (uc < 58)))) {
51  valid = false;
52  break;
53  }
54  }
55  } else {
56  valid = value.contains(QRegularExpression(QStringLiteral("^[\\pL\\pM\\pN]+$")));
57  }
58  return valid;
59 }
60 
61 QString ValidatorAlphaNum::genericValidationError(Context *c, const QVariant &errorData) const
62 {
63  QString error;
64  Q_UNUSED(errorData)
65  Q_D(const ValidatorAlphaNum);
66  const QString _label = label(c);
67  if (_label.isEmpty()) {
68  if (d->asciiOnly) {
69  error = c->translate("Cutelyst::ValidatorAlphaNum", "Must only contain alpha-numeric latin characters.");
70  } else {
71  error = c->translate("Cutelyst::ValidatorAlphaNum", "Must only contain alpha-numeric characters.");
72  }
73  } else {
74  if (d->asciiOnly) {
75  //: %1 will be replaced by the field label
76  error = c->translate("Cutelyst::ValidatorAlphaNum", "The text in the “%1” field must only contain alpha-numeric latin characters.").arg(_label);
77  } else {
78  //: %1 will be replaced by the field label
79  error = c->translate("Cutelyst::ValidatorAlphaNum", "The text in the “%1” field must only contain alpha-numeric characters.").arg(_label);
80  }
81  }
82  return error;
83 }
The Cutelyst Context.
Definition: context.h:39
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
Checks a value for only alpha-numeric content.
ValidatorAlphaNum(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new alpha num validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message.
~ValidatorAlphaNum() override
Deconstructs the alpha num validator.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString field() const
Returns the name of the field to validate.
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 ...
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
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 alpha-numeric characters.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49