cutelyst 3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
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
8using namespace Cutelyst;
9
11 QMetaType::Type type,
12 const Cutelyst::ValidatorMessages &messages,
13 const QString &defValKey)
14 : ValidatorRule(*new ValidatorIntegerPrivate(field, type, messages, defValKey))
15{
16}
17
21
23 const ParamsMultiMap &params) const
24{
26
27 const QString v = value(params);
28
29 if (!v.isEmpty()) {
30 Q_D(const ValidatorInteger);
31 QVariant converted;
32
33 switch (d->type) {
34 case QMetaType::Char:
36 case QMetaType::Int:
37 case QMetaType::Long:
41 case QMetaType::UInt:
44 converted = d->valueToNumber(c, v, d->type);
45 break;
46 default:
48 qCWarning(
49 C_VALIDATOR,
50 "ValidatorInteger: Conversion type for field %s at %s::%s is not an integer type.",
51 qPrintable(field()),
52 qPrintable(c->controllerName()),
53 qPrintable(c->actionName()));
54 break;
55 }
56
57 if (converted.isValid()) {
58 result.value = converted;
59 } else {
60 qCDebug(
61 C_VALIDATOR,
62 "ValidatorInteger: Validation failed for field %s at %s::%s: not an integer value.",
63 qPrintable(field()),
64 qPrintable(c->controllerName()),
65 qPrintable(c->actionName()));
66 result.errorMessage = validationError(c);
67 }
68 } else {
69 defaultValue(c, &result, "ValidatorInteger");
70 }
71
72 return result;
73}
74
76{
77 QString error;
78 Q_UNUSED(errorData)
79 Q_D(const ValidatorInteger);
80 const QString _label = label(c);
81 QString min;
82 QString max;
83 switch (d->type) {
84 case QMetaType::Char:
85 min = c->locale().toString(std::numeric_limits<char>::min());
86 max = c->locale().toString(std::numeric_limits<char>::max());
87 break;
89 min = c->locale().toString(std::numeric_limits<short>::min());
90 max = c->locale().toString(std::numeric_limits<short>::max());
91 break;
92 case QMetaType::Int:
93 min = c->locale().toString(std::numeric_limits<int>::min());
94 max = c->locale().toString(std::numeric_limits<int>::max());
95 break;
96 case QMetaType::Long:
97 min = c->locale().toString(static_cast<qlonglong>(std::numeric_limits<long>::min()));
98 max = c->locale().toString(static_cast<qlonglong>(std::numeric_limits<long>::max()));
99 break;
101 min = c->locale().toString(std::numeric_limits<qlonglong>::min());
102 max = c->locale().toString(std::numeric_limits<qlonglong>::max());
103 break;
104 case QMetaType::UChar:
105 min = c->locale().toString(std::numeric_limits<uchar>::min());
106 max = c->locale().toString(std::numeric_limits<uchar>::max());
107 break;
109 min = c->locale().toString(std::numeric_limits<ushort>::min());
110 max = c->locale().toString(std::numeric_limits<ushort>::max());
111 break;
112 case QMetaType::UInt:
113 min = c->locale().toString(std::numeric_limits<uint>::min());
114 max = c->locale().toString(std::numeric_limits<uint>::max());
115 break;
116 case QMetaType::ULong:
117 min = c->locale().toString(static_cast<qulonglong>(std::numeric_limits<ulong>::min()));
118 max = c->locale().toString(static_cast<qulonglong>(std::numeric_limits<ulong>::max()));
119 break;
121 default:
122 min = c->locale().toString(std::numeric_limits<qulonglong>::min());
123 max = c->locale().toString(std::numeric_limits<qulonglong>::max());
124 break;
125 }
126 if (_label.isEmpty()) {
127 //: %1 is the minimum numerical limit for the selected type, %2 is the maximum numeric limit
128 error = c->translate("Cutelyst::ValidatorInteger",
129 "Not a valid integer value between %1 and %2.")
130 .arg(min, max);
131 } else {
132 //: %1 will be replaced by the field name, %2 is the minimum numerical limit for the
133 //: selected type, %3 is the maximum numeric limit
134 error =
135 c->translate("Cutelyst::ValidatorInteger",
136 "The value in the “%1“ field is not a valid integer between %2 and %3.")
137 .arg(_label, min, max);
138 }
139 return error;
140}
The Cutelyst Context.
Definition context.h:39
QLocale locale() const noexcept
Definition context.cpp:466
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition context.cpp:490
~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.
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 ...
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
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
QMultiMap< QString, QString > ParamsMultiMap
QString toString(QDate date, QLocale::FormatType format) const const
QString arg(Args &&... args) 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.