cutelyst 4.3.0
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-2023 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
19
21 const ParamsMultiMap &params) const
22{
24
25 const QString v = value(params);
26
27 if (!v.isEmpty()) {
28 Q_D(const ValidatorInteger);
29 QVariant converted;
30
31 switch (d->type) {
32 case QMetaType::Char:
34 case QMetaType::Int:
35 case QMetaType::Long:
39 case QMetaType::UInt:
42 converted = d->valueToNumber(c, v, d->type);
43 break;
44 default:
46 qCWarning(C_VALIDATOR).noquote()
47 << debugString(c) << "Conversion type" << d->type << "is not an integer type";
48 break;
49 }
50
51 if (converted.isValid()) {
52 result.value = converted;
53 } else {
54 qCDebug(C_VALIDATOR).noquote().nospace()
55 << debugString(c) << " \"" << v << "\" is not parseable as integer value "
56 << "or exceeds the limits of the selected type " << d->type;
57 result.errorMessage = validationError(c);
58 }
59 } else {
60 defaultValue(c, &result);
61 }
62
63 return result;
64}
65
67{
68 Q_UNUSED(errorData)
69 Q_D(const ValidatorInteger);
70 const QString _label = label(c);
71 QString min;
72 QString max;
73 switch (d->type) {
74 case QMetaType::Char:
75 min = c->locale().toString(std::numeric_limits<char>::min());
76 max = c->locale().toString(std::numeric_limits<char>::max());
77 break;
79 min = c->locale().toString(std::numeric_limits<short>::min());
80 max = c->locale().toString(std::numeric_limits<short>::max());
81 break;
82 case QMetaType::Int:
83 min = c->locale().toString(std::numeric_limits<int>::min());
84 max = c->locale().toString(std::numeric_limits<int>::max());
85 break;
86 case QMetaType::Long:
87 min = c->locale().toString(static_cast<qlonglong>(std::numeric_limits<long>::min()));
88 max = c->locale().toString(static_cast<qlonglong>(std::numeric_limits<long>::max()));
89 break;
91 min = c->locale().toString(std::numeric_limits<qlonglong>::min());
92 max = c->locale().toString(std::numeric_limits<qlonglong>::max());
93 break;
95 min = c->locale().toString(std::numeric_limits<uchar>::min());
96 max = c->locale().toString(std::numeric_limits<uchar>::max());
97 break;
99 min = c->locale().toString(std::numeric_limits<ushort>::min());
100 max = c->locale().toString(std::numeric_limits<ushort>::max());
101 break;
102 case QMetaType::UInt:
103 min = c->locale().toString(std::numeric_limits<uint>::min());
104 max = c->locale().toString(std::numeric_limits<uint>::max());
105 break;
106 case QMetaType::ULong:
107 min = c->locale().toString(static_cast<qulonglong>(std::numeric_limits<ulong>::min()));
108 max = c->locale().toString(static_cast<qulonglong>(std::numeric_limits<ulong>::max()));
109 break;
111 default:
112 min = c->locale().toString(std::numeric_limits<qulonglong>::min());
113 max = c->locale().toString(std::numeric_limits<qulonglong>::max());
114 break;
115 }
116 if (_label.isEmpty()) {
117 //: %1 is the minimum numerical limit for the selected type, %2 is the maximum numeric limit
118 //% "Not a valid integer value between %1 and %2."
119 return c->qtTrId("cutelyst-valinteger-genvalerr").arg(min, max);
120 } else {
121 //: %1 will be replaced by the field name, %2 is the minimum numerical limit for the
122 //: selected type, %3 is the maximum numeric limit
123 //% "The value in the “%1“ field is not a valid integer between %2 and %3."
124 return c->qtTrId("cutelyst-valinteger-genvalerr-label").arg(_label, min, max);
125 }
126}
The Cutelyst Context.
Definition context.h:42
QLocale locale() const noexcept
Definition context.cpp:460
QString qtTrId(const char *id, int n=-1) const
Definition context.h:656
Checks if the value is an integer.
ValidatorInteger(const QString &field, QMetaType::Type type=QMetaType::ULongLong, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
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 validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
The Cutelyst namespace holds all public Cutelyst API.
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.