Cutelyst  2.13.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  max = 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  max = 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  max = 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  max = 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  max = 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  max = 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  max = 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  max = 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  max = 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("Cutelyst::ValidatorInteger", "Not a valid integer value between %1 and %2.").arg(min, max);
128  } else {
129  //: %1 will be replaced by the field name, %2 is the minimum numerical limit for the selected type, %3 is the maximum numeric limit
130  error = c->translate("Cutelyst::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 }
Cutelyst::ParamsMultiMap
QMap< QString, QString > ParamsMultiMap
Definition: paramsmultimap.h:36
Cutelyst::ValidatorInteger::genericValidationError
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Definition: validatorinteger.cpp:74
Cutelyst::ValidatorMessages
Stores custom error messages and the input field label.
Definition: validatorrule.h:144
Cutelyst::ValidatorRule::value
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
Definition: validatorrule.cpp:41
Cutelyst::ValidatorRule::label
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
Definition: validatorrule.cpp:58
Cutelyst::ValidatorInteger::~ValidatorInteger
~ValidatorInteger() override
Deconstructs the integer validator.
Definition: validatorinteger.cpp:28
Cutelyst::ValidatorRule::validationError
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Definition: validatorrule.cpp:72
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:50
Cutelyst::ValidatorInteger::ValidatorInteger
ValidatorInteger(const QString &field, QMetaType::Type type=QMetaType::ULongLong, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new integer validator.
Definition: validatorinteger.cpp:23
Cutelyst::ValidatorRule
Base class for all validator rules.
Definition: validatorrule.h:292
Cutelyst::ValidatorRule::field
QString field() const
Returns the name of the field to validate.
Definition: validatorrule.cpp:39
Cutelyst::ValidatorInteger::validate
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
Definition: validatorinteger.cpp:32
Cutelyst::Context::translate
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:473
Cutelyst::ValidatorReturnType
Contains the result of a single input parameter validation.
Definition: validatorrule.h:62
Cutelyst::Context::locale
QLocale locale() const
Definition: context.cpp:449
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::ValidatorReturnType::value
QVariant value
Definition: validatorrule.h:64
Cutelyst::ValidatorInteger
Checks if the value is an integer.
Definition: validatorinteger.h:47
Cutelyst::ValidatorReturnType::errorMessage
QString errorMessage
Definition: validatorrule.h:63
Cutelyst::ValidatorRule::validationDataError
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
Definition: validatorrule.cpp:132
Cutelyst::ValidatorRule::defaultValue
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 ...
Definition: validatorrule.cpp:162