cutelyst  3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatordigits.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatordigits_p.h"
7 
8 using namespace Cutelyst;
9 
11  const QVariant &length,
12  const Cutelyst::ValidatorMessages &messages,
13  const QString &defValKey)
14  : ValidatorRule(*new ValidatorDigitsPrivate(field, length, messages, defValKey))
15 {
16 }
17 
19 {
20 }
21 
23 {
24  ValidatorReturnType result;
25 
26  Q_D(const ValidatorDigits);
27 
28  const QString v = value(params);
29  bool ok = false;
30  int _length = d->extractInt(c, params, d->length, &ok);
31  if (!ok) {
33  return result;
34  }
35 
36  if (!v.isEmpty()) {
37 
38  if (Q_LIKELY(ValidatorDigits::validate(v, _length))) {
39  if ((_length > 0) && (v.length() != _length)) {
40  result.errorMessage = validationError(c, _length);
41  qCDebug(C_VALIDATOR,
42  "ValidatorDigits: Validation failed for value \"%s\" in field %s at "
43  "%s::%s: does not contain exactly %i digit(s).",
44  qPrintable(v),
45  qPrintable(field()),
46  qPrintable(c->controllerName()),
47  qPrintable(c->actionName()),
48  _length);
49  } else {
50  result.value.setValue(v);
51  }
52  } else {
53  result.errorMessage = validationError(c, _length);
54  qCDebug(C_VALIDATOR,
55  "ValidatorDigits: Validation failed for value \"%s\" in field %s at %s::%s: "
56  "does not only contain digits.",
57  qPrintable(v),
58  qPrintable(field()),
59  qPrintable(c->controllerName()),
60  qPrintable(c->actionName()));
61  }
62 
63  } else {
64  defaultValue(c, &result, "ValidatorDigits");
65  }
66 
67  return result;
68 }
69 
70 bool ValidatorDigits::validate(const QString &value, int length)
71 {
72  bool valid = true;
73 
74  for (const QChar &ch : value) {
75  const ushort &uc = ch.unicode();
76  if (!((uc > 47) && (uc < 58))) {
77  valid = false;
78  break;
79  }
80  }
81 
82  if (valid && (length > 0) && (length != value.length())) {
83  valid = false;
84  }
85 
86  return valid;
87 }
88 
90 {
91  QString error;
92 
93  const QString _label = label(c);
94  const int _length = errorData.toInt();
95 
96  if (_label.isEmpty()) {
97  if (_length > 0) {
98  error = c->translate(
99  "Cutelyst::ValidatorDigits", "Must contain exactly %n digit(s).", "", _length);
100  } else {
101  error = c->translate("Cutelyst::ValidatorDigits", "Must only contain digits.");
102  }
103  } else {
104  if (_length > 0) {
105  //: %1 will be replaced by the field label
106  error = c->translate("Cutelyst::ValidatorDigits",
107  "The “%1” field must contain exactly %n digit(s).",
108  "",
109  _length)
110  .arg(_label);
111  } else {
112  //: %1 will be replaced by the field label
113  error = c->translate("Cutelyst::ValidatorDigits",
114  "The “%1” field must only contain digits.")
115  .arg(_label);
116  }
117  }
118 
119  return error;
120 }
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Checks for digits only with optional length check.
~ValidatorDigits() override
Deconstructs the digits validator.
Stores custom error messages and the input field label.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
The Cutelyst Context.
Definition: context.h:38
int toInt(bool *ok) const const
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:490
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
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.
static bool validate(const QString &value, int length=-1)
Returns true if value only contains digits.
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
ValidatorDigits(const QString &field, const QVariant &length=-1, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new digits validator.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
int length() 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
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 ...