Cutelyst  2.5.0
validatordigitsbetween.cpp
1 /*
2  * Copyright (C) 2017-2018 Matthias Fehring <kontakt@buschmann23.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "validatordigitsbetween_p.h"
20 
21 using namespace Cutelyst;
22 
23 ValidatorDigitsBetween::ValidatorDigitsBetween(const QString &field, const QVariant &min, const QVariant &max, const ValidatorMessages &messages, const QString &defValKey) :
24  ValidatorRule(*new ValidatorDigitsBetweenPrivate(field, min, max, messages, defValKey))
25 {
26 }
27 
29 {
30 }
31 
33 {
34  ValidatorReturnType result;
35 
36  Q_D(const ValidatorDigitsBetween);
37 
38  const QString v = value(params);
39 
40  bool ok = false;
41  int _max = 0;
42  int _min = d->extractInt(c, params, d->min, &ok);
43  if (!ok) {
45  qCWarning(C_VALIDATOR, "ValidatorDigitsBetween: Invalid minimum validation length for field %s at %s::%s.", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
46  return result;
47  } else {
48  _max = d->extractInt(c, params, d->max, &ok);
49  if (!ok) {
51  qCWarning(C_VALIDATOR, "ValidatorDigitsBetween: Invalid maximum validation length for field %s at %s::%s.", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
52  return result;
53  }
54  }
55 
56  if (_min > _max) {
58  qCWarning(C_VALIDATOR, "ValidatorDigitsBetween: Minimum length %i is larger than maximum length %i for field %s at %s::%s", _min, _max, qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
59  return result;
60  }
61 
62  if (!v.isEmpty()) {
63 
64  if (Q_LIKELY(ValidatorDigitsBetween::validate(v, _min, _max))) {
65  result.value.setValue<QString>(v);
66  } else {
67  result.errorMessage = validationError(c, QVariantList{_min, _max});
68  qCDebug(C_VALIDATOR, "ValidatorDigitsBetween: Validation failed for value \"%s\" in field %s at %s::%s: length not between %i and %i and/or non-digit characters.", qPrintable(v), qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), _min, _max);
69  }
70 
71  } else {
72  defaultValue(c, &result, "ValidatorDigitsBetween");
73  }
74 
75  return result;
76 }
77 
78 bool ValidatorDigitsBetween::validate(const QString &value, int min, int max)
79 {
80  bool valid = true;
81 
82  for (const QChar &ch : value) {
83  const ushort &uc = ch.unicode();
84  if (!((uc > 47) && (uc < 58))) {
85  valid = false;
86  break;
87  }
88  }
89 
90  if (valid && ((value.length() < min) || (value.length() > max))) {
91  valid = false;
92  }
93 
94  return valid;
95 }
96 
97 QString ValidatorDigitsBetween::genericValidationError(Context *c, const QVariant &errorData) const
98 {
99  QString error;
100 
101  const QVariantList list = errorData.toList();
102  const QString min = list.at(0).toString();
103  const QString max = list.at(1).toString();
104  const QString _label = label(c);
105 
106  if (_label.isEmpty()) {
107  error = c->translate("Cutelyst::ValidatorDigitsBetween", "Must contain between %1 and %2 digits.").arg(min, max);
108  } else {
109  error = c->translate("Cutelyst::ValidatorDigitsBetween", "The “%1” field must contain between %2 and %3 digits.").arg(_label, min, max);
110  }
111 
112  return error;
113 }
QMap< QString, QString > ParamsMultiMap
ValidatorDigitsBetween(const QString &field, const QVariant &min, const QVariant &max, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new digits between validator.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
~ValidatorDigitsBetween()
Deconstructs the digits between validator.
Stores custom error messages and the input field label.
static bool validate(const QString &value, int min, int max)
Returns true if value only contains digits and has a length between min and max.
The Cutelyst Context.
Definition: context.h:50
Checks for digits only with a length between min and max.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:414
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.
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:62
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 ...