Cutelyst  2.3.0
validatorinteger.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 "validatorinteger_p.h"
20 
21 using namespace Cutelyst;
22 
23 ValidatorInteger::ValidatorInteger(const QString &field, QMetaType::Type type, const Cutelyst::ValidatorMessages &messages, const QString &defValKey) :
24  ValidatorRule(*new ValidatorIntegerPrivate(field, type, messages, defValKey))
25 {
26 }
27 
29 {
30 }
31 
33 {
34  ValidatorReturnType result;
35 
36  const QString v = value(params);
37 
38  if (!v.isEmpty()) {
39  Q_D(const ValidatorInteger);
40  QVariant converted;
41 
42  switch(d->type) {
43  case QMetaType::Char:
44  case QMetaType::Short:
45  case QMetaType::Int:
46  case QMetaType::Long:
47  case QMetaType::LongLong:
48  case QMetaType::UChar:
49  case QMetaType::UShort:
50  case QMetaType::UInt:
51  case QMetaType::ULong:
52  case QMetaType::ULongLong:
53  converted = d->valueToNumber(c, v, d->type);
54  break;
55  default:
57  qCWarning(C_VALIDATOR, "ValidatorInteger: Conversion type for field %s at %s::%s is not an integer type.", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
58  break;
59  }
60 
61  if (converted.isValid()) {
62  result.value = converted;
63  } else {
64  qCDebug(C_VALIDATOR, "ValidatorInteger: Validation failed for field %s at %s::%s: not an integer value.", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
65  result.errorMessage = validationError(c);
66  }
67  } else {
68  defaultValue(c, &result, "ValidatorInteger");
69  }
70 
71  return result;
72 }
73 
74 QString ValidatorInteger::genericValidationError(Context *c, const QVariant &errorData) const
75 {
76  QString error;
77  Q_UNUSED(errorData)
78  Q_D(const ValidatorInteger);
79  const QString _label = label(c);
80  QString min;
81  QString max;
82  switch (d->type) {
83  case QMetaType::Char:
84  min = c->locale().toString(std::numeric_limits<char>::min());
85  max = c->locale().toString(std::numeric_limits<char>::max());
86  break;
87  case QMetaType::Short:
88  min = c->locale().toString(std::numeric_limits<short>::min());
89  min = c->locale().toString(std::numeric_limits<short>::max());
90  break;
91  case QMetaType::Int:
92  min = c->locale().toString(std::numeric_limits<int>::min());
93  min = c->locale().toString(std::numeric_limits<int>::max());
94  break;
95  case QMetaType::Long:
96  min = c->locale().toString(static_cast<qlonglong>(std::numeric_limits<long>::min()));
97  min = c->locale().toString(static_cast<qlonglong>(std::numeric_limits<long>::max()));
98  break;
99  case QMetaType::LongLong:
100  min = c->locale().toString(std::numeric_limits<qlonglong>::min());
101  min = c->locale().toString(std::numeric_limits<qlonglong>::max());
102  break;
103  case QMetaType::UChar:
104  min = c->locale().toString(std::numeric_limits<uchar>::min());
105  min = c->locale().toString(std::numeric_limits<uchar>::max());
106  break;
107  case QMetaType::UShort:
108  min = c->locale().toString(std::numeric_limits<ushort>::min());
109  min = c->locale().toString(std::numeric_limits<ushort>::max());
110  break;
111  case QMetaType::UInt:
112  min = c->locale().toString(std::numeric_limits<uint>::min());
113  min = c->locale().toString(std::numeric_limits<uint>::max());
114  break;
115  case QMetaType::ULong:
116  min = c->locale().toString(static_cast<qulonglong>(std::numeric_limits<ulong>::min()));
117  min = c->locale().toString(static_cast<qulonglong>(std::numeric_limits<ulong>::max()));
118  break;
119  case QMetaType::ULongLong:
120  default:
121  min = c->locale().toString(std::numeric_limits<qulonglong>::min());
122  min = c->locale().toString(std::numeric_limits<qulonglong>::max());
123  break;
124  }
125  if (_label.isEmpty()) {
126  //: %1 is the minimum numerical limit for the selected type, %2 is the maximum numeric limit
127  error = c->translate("ValidatorInteger", "Not a valid integer value between %1 and %2.").arg(min, max);
128  } else {
129  //: %2 is the minimum numerical limit for the selected type, %3 is the maximum numeric limit
130  error = c->translate("ValidatorInteger", "The value in the “%1“ field is not a valid integer between %2 and %3.").arg(_label, min, max);
131  }
132  return error;
133 }
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
QMap< QString, QString > ParamsMultiMap
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
ValidatorInteger(const QString &field, QMetaType::Type type=QMetaType::ULongLong, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new integer validator.
Stores custom error messages and the input field label.
~ValidatorInteger()
Deconstructs the integer validator.
Checks if the value is an integer.
QLocale locale() const
Definition: context.cpp:390
The Cutelyst Context.
Definition: context.h:50
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
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 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 ...