Cutelyst  3.1.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:
48  case QMetaType::UChar:
49  case QMetaType::UShort:
50  case QMetaType::UInt:
51  case QMetaType::ULong:
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 
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;
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;
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 }
The Cutelyst Context.
Definition: context.h:52
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:480
QLocale locale() const
Definition: context.cpp:456
Checks if the value is an integer.
~ValidatorInteger() override
Deconstructs the integer validator.
ValidatorInteger(const QString &field, QMetaType::Type type=QMetaType::ULongLong, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new integer validator.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString field() const
Returns the name of the field to validate.
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 ...
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
QString toString(qlonglong i) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
bool isValid() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:62