6#include "validatormax_p.h"
14 const QString &defValKey)
15 :
ValidatorRule(*new ValidatorMaxPrivate(field, type, max, messages, defValKey))
27 const QString v =
value(params);
36 case QMetaType::Short:
39 case QMetaType::LongLong:
41 const qlonglong val = c->
locale().toLongLong(v, &ok);
42 if (Q_UNLIKELY(!ok)) {
44 qCWarning(C_VALIDATOR,
45 "ValidatorMax: Failed to parse value of field %s into number at %s::%s.",
47 qPrintable(c->controllerName()),
48 qPrintable(c->actionName()));
50 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
51 if (Q_UNLIKELY(!ok)) {
55 "ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.",
57 qPrintable(c->controllerName()),
58 qPrintable(c->actionName()));
63 QVariantMap{{QStringLiteral(
"val"), val},
64 {QStringLiteral(
"max"), max}});
66 "ValidatorMax: Validation failed for field %s in %s::%s: %lli is "
67 "not smaller than %lli.",
69 qPrintable(c->controllerName()),
70 qPrintable(c->actionName()),
79 case QMetaType::UChar:
80 case QMetaType::UShort:
82 case QMetaType::ULong:
83 case QMetaType::ULongLong:
85 const qulonglong val = v.toULongLong(&ok);
86 if (Q_UNLIKELY(!ok)) {
88 qCWarning(C_VALIDATOR,
89 "ValidatorMax: Failed to parse value of field %s into number at %s::%s.",
91 qPrintable(c->controllerName()),
92 qPrintable(c->actionName()));
94 const qulonglong max = d->extractULongLong(c, params, d->max, &ok);
95 if (Q_UNLIKELY(!ok)) {
99 "ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.",
101 qPrintable(c->controllerName()),
102 qPrintable(c->actionName()));
107 QVariantMap{{QStringLiteral(
"val"), val},
108 {QStringLiteral(
"max"), max}});
110 "ValidatorMax: Validation failed for field %s in %s::%s: %llu is "
111 "not smaller than %llu.",
113 qPrintable(c->controllerName()),
114 qPrintable(c->actionName()),
123 case QMetaType::Float:
124 case QMetaType::Double:
126 const double val = v.toDouble(&ok);
127 if (Q_UNLIKELY(!ok)) {
129 qCWarning(C_VALIDATOR,
130 "ValidatorMax: Failed to parse value of field %s into number at %s::%s.",
132 qPrintable(c->controllerName()),
133 qPrintable(c->actionName()));
135 const double max = d->extractDouble(c, params, d->max, &ok);
136 if (Q_UNLIKELY(!ok)) {
140 "ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.",
142 qPrintable(c->controllerName()),
143 qPrintable(c->actionName()));
148 QVariantMap{{QStringLiteral(
"val"), val},
149 {QStringLiteral(
"max"), max}});
151 "ValidatorMax: Validation failed for field %s in %s::%s: %f is not "
154 qPrintable(c->controllerName()),
155 qPrintable(c->actionName()),
164 case QMetaType::QString:
166 const qlonglong val =
static_cast<qlonglong
>(v.length());
167 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
168 if (Q_UNLIKELY(!ok)) {
170 qCWarning(C_VALIDATOR,
171 "ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.",
173 qPrintable(c->controllerName()),
174 qPrintable(c->actionName()));
178 c, QVariantMap{{QStringLiteral(
"val"), val}, {QStringLiteral(
"max"), max}});
180 "ValidatorMax: Validation failed for field %s in %s::%s: string length "
181 "%lli is not smaller than %lli.",
183 qPrintable(c->controllerName()),
184 qPrintable(c->actionName()),
193 qCWarning(C_VALIDATOR,
194 "ValidatorMax: The comparison type with ID %i for field %s at %s::%s is not "
196 static_cast<int>(d->type),
198 qPrintable(c->controllerName()),
199 qPrintable(c->actionName()));
205 if (d->type != QMetaType::QString) {
206 const QVariant _v = d->valueToNumber(c, v, d->type);
213 result.
value.setValue(v);
229 const QVariantMap map = errorData.toMap();
232 case QMetaType::Char:
233 case QMetaType::Short:
235 case QMetaType::Long:
236 case QMetaType::LongLong:
237 case QMetaType::QString:
238 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toLongLong());
240 case QMetaType::UChar:
241 case QMetaType::UShort:
242 case QMetaType::UInt:
243 case QMetaType::ULong:
244 case QMetaType::ULongLong:
245 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toULongLong());
247 case QMetaType::Float:
248 case QMetaType::Double:
249 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toDouble());
256 const QString _label =
label(c);
258 if (_label.isEmpty()) {
259 if (d->type == QMetaType::QString) {
260 error = c->
translate(
"Cutelyst::ValidatorMax",
261 "The text must be shorter than %1 characters.")
265 c->
translate(
"Cutelyst::ValidatorMax",
"The value must be lower than %1.").arg(max);
268 if (d->type == QMetaType::QString) {
269 error = c->
translate(
"Cutelyst::ValidatorMax",
270 "The text in the “%1“ field must be shorter than %2 characters.")
273 error = c->
translate(
"Cutelyst::ValidatorMax",
274 "The value in the “%1” field must be lower than %2.")
286 int field = errorData.toInt();
287 const QString _label =
label(c);
291 if (_label.isEmpty()) {
292 error = c->
translate(
"Cutelyst::ValidatorMax",
293 "The comparison type with ID %1 is not supported.")
294 .arg(
static_cast<int>(d->type));
298 "The comparison type with ID %1 for the “%2” field is not supported.")
299 .arg(QString::number(
static_cast<int>(d->type)), _label);
301 }
else if (
field == 1) {
302 if (_label.isEmpty()) {
303 error = c->
translate(
"Cutelyst::ValidatorMax",
304 "The maximum comparison value is not valid.");
306 error = c->
translate(
"Cutelyst::ValidatorMax",
307 "The maximum comparison value for the “%1” field is not valid.")
321 const QString _label =
label(c);
322 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
323 if (_label.isEmpty()) {
324 error = c->
translate(
"Cutelyst::ValidatorMax",
325 "Failed to parse the input value into a floating point number.");
327 error = c->
translate(
"Cutelyst::ValidatorMax",
328 "Failed to parse the input value for the “%1” field into a "
329 "floating point number.")
333 if (_label.isEmpty()) {
334 error = c->
translate(
"Cutelyst::ValidatorMax",
335 "Failed to parse the input value into an integer number.");
339 "Cutelyst::ValidatorMax",
340 "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 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.
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 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 ¶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.
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.