6#include "validatormax_p.h"
14 const QString &defValKey)
15 :
ValidatorRule(*new ValidatorMaxPrivate(field, type, max, messages, defValKey))
25 const QString v =
value(params);
34 case QMetaType::Short:
37 case QMetaType::LongLong:
39 const qlonglong val = c->
locale().toLongLong(v, &ok);
40 if (Q_UNLIKELY(!ok)) {
42 qCWarning(C_VALIDATOR).noquote().nospace()
43 <<
debugString(c) <<
" Failed to parse \"" << v <<
"\" into an integer number";
45 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
46 if (Q_UNLIKELY(!ok)) {
48 qCWarning(C_VALIDATOR).noquote()
49 <<
debugString(c) <<
"Invalid maximum comparison value";
54 QVariantMap{{QStringLiteral(
"val"), val},
55 {QStringLiteral(
"max"), max}});
56 qCDebug(C_VALIDATOR).noquote()
57 <<
debugString(c) << val <<
"is not smaller than" << max;
64 case QMetaType::UChar:
65 case QMetaType::UShort:
67 case QMetaType::ULong:
68 case QMetaType::ULongLong:
70 const qulonglong val = v.toULongLong(&ok);
71 if (Q_UNLIKELY(!ok)) {
73 qCWarning(C_VALIDATOR).noquote().nospace()
75 <<
"\" into an unsigned integer number";
77 const qulonglong max = d->extractULongLong(c, params, d->max, &ok);
78 if (Q_UNLIKELY(!ok)) {
80 qCWarning(C_VALIDATOR).noquote()
81 <<
debugString(c) <<
"Invalid maximum comparison value";
86 QVariantMap{{QStringLiteral(
"val"), val},
87 {QStringLiteral(
"max"), max}});
88 qCDebug(C_VALIDATOR).noquote()
89 <<
debugString(c) << val <<
"is not smaller than" << max;
96 case QMetaType::Float:
97 case QMetaType::Double:
99 const double val = v.toDouble(&ok);
100 if (Q_UNLIKELY(!ok)) {
102 qCWarning(C_VALIDATOR).noquote().nospace()
104 <<
"\" into a floating point number";
106 const double max = d->extractDouble(c, params, d->max, &ok);
107 if (Q_UNLIKELY(!ok)) {
109 qCWarning(C_VALIDATOR).noquote()
110 <<
debugString(c) <<
"Invalid maximum comparison value";
115 QVariantMap{{QStringLiteral(
"val"), val},
116 {QStringLiteral(
"max"), max}});
117 qCDebug(C_VALIDATOR).noquote()
118 <<
debugString(c) << val <<
"is not smaller than" << max;
125 case QMetaType::QString:
127 const auto val =
static_cast<qlonglong
>(v.length());
128 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
129 if (Q_UNLIKELY(!ok)) {
131 qCWarning(C_VALIDATOR).noquote()
132 <<
debugString(c) <<
"Invalid maximum comparison value";
136 c, QVariantMap{{QStringLiteral(
"val"), val}, {QStringLiteral(
"max"), max}});
137 qCDebug(C_VALIDATOR).noquote()
138 <<
debugString(c) <<
"String length" << val <<
"is not shorter than" << max;
145 qCWarning(C_VALIDATOR).noquote()
146 <<
debugString(c) <<
"The comparison type" << d->type <<
"is not supported";
152 if (d->type != QMetaType::QString) {
153 const QVariant _v = d->valueToNumber(c, v, d->type);
160 result.
value.setValue(v);
176 const QVariantMap map = errorData.toMap();
179 case QMetaType::Char:
180 case QMetaType::Short:
182 case QMetaType::Long:
183 case QMetaType::LongLong:
184 case QMetaType::QString:
185 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toLongLong());
187 case QMetaType::UChar:
188 case QMetaType::UShort:
189 case QMetaType::UInt:
190 case QMetaType::ULong:
191 case QMetaType::ULongLong:
192 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toULongLong());
194 case QMetaType::Float:
195 case QMetaType::Double:
196 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toDouble());
203 const QString _label =
label(c);
205 if (_label.isEmpty()) {
206 if (d->type == QMetaType::QString) {
207 error = c->
translate(
"Cutelyst::ValidatorMax",
208 "The text must be shorter than %1 characters.")
212 c->
translate(
"Cutelyst::ValidatorMax",
"The value must be lower than %1.").arg(max);
215 if (d->type == QMetaType::QString) {
216 error = c->
translate(
"Cutelyst::ValidatorMax",
217 "The text in the “%1“ field must be shorter than %2 characters.")
220 error = c->
translate(
"Cutelyst::ValidatorMax",
221 "The value in the “%1” field must be lower than %2.")
233 int field = errorData.toInt();
234 const QString _label =
label(c);
238 if (_label.isEmpty()) {
239 error = c->
translate(
"Cutelyst::ValidatorMax",
240 "The comparison type with ID %1 is not supported.")
241 .arg(
static_cast<int>(d->type));
245 "The comparison type with ID %1 for the “%2” field is not supported.")
246 .arg(QString::number(
static_cast<int>(d->type)), _label);
248 }
else if (
field == 1) {
249 if (_label.isEmpty()) {
250 error = c->
translate(
"Cutelyst::ValidatorMax",
251 "The maximum comparison value is not valid.");
253 error = c->
translate(
"Cutelyst::ValidatorMax",
254 "The maximum comparison value for the “%1” field is not valid.")
268 const QString _label =
label(c);
269 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
270 if (_label.isEmpty()) {
271 error = c->
translate(
"Cutelyst::ValidatorMax",
272 "Failed to parse the input value into a floating point number.");
274 error = c->
translate(
"Cutelyst::ValidatorMax",
275 "Failed to parse the input value for the “%1” field into a "
276 "floating point number.")
280 if (_label.isEmpty()) {
281 error = c->
translate(
"Cutelyst::ValidatorMax",
282 "Failed to parse the input value into an integer number.");
286 "Cutelyst::ValidatorMax",
287 "Failed to parse the input value for the “%1” field into an integer number.")
QLocale locale() const noexcept
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Checks if a value is not bigger or longer than a maximum value.
~ValidatorMax() override
Deconstructs the max validator.
ValidatorMax(const QString &field, QMetaType::Type type, const QVariant &max, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new max validator.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error message for validation data errors.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message.
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
QString genericParsingError(Context *c, const QVariant &errorData) const override
Returns a generic error message for input value parsing errors.
Base class for all validator rules.
QString field() const noexcept
Returns the name of the field to validate.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occurred while parsing input.
void defaultValue(Context *c, ValidatorReturnType *result) 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 ¶ms) 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.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.