Cutelyst  3.5.0
validatorinteger.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorinteger_p.h"
7 
8 using namespace Cutelyst;
9 
10 ValidatorInteger::ValidatorInteger(const QString &field, QMetaType::Type type, const Cutelyst::ValidatorMessages &messages, const QString &defValKey) :
11  ValidatorRule(*new ValidatorIntegerPrivate(field, type, messages, defValKey))
12 {
13 }
14 
16 {
17 }
18 
20 {
21  ValidatorReturnType result;
22 
23  const QString v = value(params);
24 
25  if (!v.isEmpty()) {
26  Q_D(const ValidatorInteger);
27  QVariant converted;
28 
29  switch(d->type) {
30  case QMetaType::Char:
31  case QMetaType::Short:
32  case QMetaType::Int:
33  case QMetaType::Long:
34  case QMetaType::LongLong:
35  case QMetaType::UChar:
36  case QMetaType::UShort:
37  case QMetaType::UInt:
38  case QMetaType::ULong:
39  case QMetaType::ULongLong:
40  converted = d->valueToNumber(c, v, d->type);
41  break;
42  default:
44  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()));
45  break;
46  }
47 
48  if (converted.isValid()) {
49  result.value = converted;
50  } else {
51  qCDebug(C_VALIDATOR, "ValidatorInteger: Validation failed for field %s at %s::%s: not an integer value.", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
52  result.errorMessage = validationError(c);
53  }
54  } else {
55  defaultValue(c, &result, "ValidatorInteger");
56  }
57 
58  return result;
59 }
60 
62 {
63  QString error;
64  Q_UNUSED(errorData)
65  Q_D(const ValidatorInteger);
66  const QString _label = label(c);
67  QString min;
68  QString max;
69  switch (d->type) {
70  case QMetaType::Char:
71  min = c->locale().toString(std::numeric_limits<char>::min());
72  max = c->locale().toString(std::numeric_limits<char>::max());
73  break;
74  case QMetaType::Short:
75  min = c->locale().toString(std::numeric_limits<short>::min());
76  max = c->locale().toString(std::numeric_limits<short>::max());
77  break;
78  case QMetaType::Int:
79  min = c->locale().toString(std::numeric_limits<int>::min());
80  max = c->locale().toString(std::numeric_limits<int>::max());
81  break;
82  case QMetaType::Long:
83  min = c->locale().toString(static_cast<qlonglong>(std::numeric_limits<long>::min()));
84  max = c->locale().toString(static_cast<qlonglong>(std::numeric_limits<long>::max()));
85  break;
86  case QMetaType::LongLong:
87  min = c->locale().toString(std::numeric_limits<qlonglong>::min());
88  max = c->locale().toString(std::numeric_limits<qlonglong>::max());
89  break;
90  case QMetaType::UChar:
91  min = c->locale().toString(std::numeric_limits<uchar>::min());
92  max = c->locale().toString(std::numeric_limits<uchar>::max());
93  break;
94  case QMetaType::UShort:
95  min = c->locale().toString(std::numeric_limits<ushort>::min());
96  max = c->locale().toString(std::numeric_limits<ushort>::max());
97  break;
98  case QMetaType::UInt:
99  min = c->locale().toString(std::numeric_limits<uint>::min());
100  max = c->locale().toString(std::numeric_limits<uint>::max());
101  break;
102  case QMetaType::ULong:
103  min = c->locale().toString(static_cast<qulonglong>(std::numeric_limits<ulong>::min()));
104  max = c->locale().toString(static_cast<qulonglong>(std::numeric_limits<ulong>::max()));
105  break;
106  case QMetaType::ULongLong:
107  default:
108  min = c->locale().toString(std::numeric_limits<qulonglong>::min());
109  max = c->locale().toString(std::numeric_limits<qulonglong>::max());
110  break;
111  }
112  if (_label.isEmpty()) {
113  //: %1 is the minimum numerical limit for the selected type, %2 is the maximum numeric limit
114  error = c->translate("Cutelyst::ValidatorInteger", "Not a valid integer value between %1 and %2.").arg(min, max);
115  } else {
116  //: %1 will be replaced by the field name, %2 is the minimum numerical limit for the selected type, %3 is the maximum numeric limit
117  error = c->translate("Cutelyst::ValidatorInteger", "The value in the “%1“ field is not a valid integer between %2 and %3.").arg(_label, min, max);
118  }
119  return error;
120 }
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
QString toString(qlonglong i) const const
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() override
Deconstructs the integer validator.
Checks if the value is an integer.
The Cutelyst Context.
Definition: context.h:38
bool isEmpty() const const
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:471
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.
QLocale locale() const noexcept
Definition: context.cpp:447
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.
bool isValid() const const
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
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 ...