Cutelyst  3.5.0
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 
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 }
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Stores custom error messages and the input field label.
The Cutelyst Context.
Definition: context.h:38
~ValidatorAlphaNum() override
Deconstructs the alpha num validator.
Checks a value for only alpha-numeric content.
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:471
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
ValidatorAlphaNum(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new 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.
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.
static bool validate(const QString &value, bool asciiOnly=false)
Returns true if value only contains alpha-numeric characters.
QString field() const
Returns the name of the field to validate.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
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 ...