cutelyst  3.7.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-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatoralphadash_p.h"
7 #include <QRegularExpression>
8 
9 using namespace Cutelyst;
10 
11 ValidatorAlphaDash::ValidatorAlphaDash(const QString &field, bool asciiOnly, const ValidatorMessages &messages, const QString &defValKey) :
12  ValidatorRule(*new ValidatorAlphaDashPrivate(field, asciiOnly, messages, defValKey))
13 {
14 
15 }
16 
18 {
19 
20 }
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, "ValidatorAlphaDash: 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  } else {
37  defaultValue(c, &result, "ValidatorAlphaDash");
38  }
39 
40  return result;
41 }
42 
43 bool ValidatorAlphaDash::validate(const QString &value, bool asciiOnly)
44 {
45  bool valid = true;
46  if (asciiOnly) {
47  for (const QChar &ch : value) {
48  const ushort &uc = ch.unicode();
49  if (!(((uc > 64) && (uc < 91)) || ((uc > 96) && (uc < 123)) || ((uc > 47) && (uc < 58)) || (uc == 45) || (uc == 95))) {
50  valid = false;
51  break;
52  }
53  }
54  } else {
55  valid = value.contains(QRegularExpression(QStringLiteral("^[\\pL\\pM\\pN_-]+$")));
56  }
57  return valid;
58 }
59 
60 QString ValidatorAlphaDash::genericValidationError(Cutelyst::Context *c, const QVariant &errorData) const
61 {
62  QString error;
63  Q_UNUSED(errorData)
64  Q_D(const ValidatorAlphaDash);
65  const QString _label = label(c);
66  if (_label.isEmpty()) {
67  if (d->asciiOnly) {
68  error = c->translate("Cutelyst::ValidatorAlphaDash", "Must only contain alpha-numeric latin characters, dashes and underscores.");
69  } else {
70  error = c->translate("Cutelyst::ValidatorAlphaDash", "Must only contain alpha-numeric characters, dashes and underscores.");
71  }
72  } else {
73  if (d->asciiOnly) {
74  //: %1 will be replaced by the field label
75  error = c->translate("Cutelyst::ValidatorAlphaDash", "The text in the “%1” field must only contain alpha-numeric latin characters, dashes and underscores.").arg(_label);
76  } else {
77  //: %1 will be replaced by the field label
78  error = c->translate("Cutelyst::ValidatorAlphaDash", "The text in the “%1” field must only contain alpha-numeric characters, dashes and underscores.").arg(_label);
79  }
80  }
81  return error;
82 }
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 and dashes and underscores.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message.
ValidatorAlphaDash(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new alpha dash validator.
~ValidatorAlphaDash() override
Deconstructs the alpha dash 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 the value only contains alpha-numeric characters, dashes and underscores.
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