6#include "validatorbetween_p.h"
15 const QString &defValKey)
16 :
ValidatorRule(*new ValidatorBetweenPrivate(field, type, min, max, messages, defValKey))
28 const QString v =
value(params);
38 case QMetaType::Short:
41 case QMetaType::LongLong:
43 const qlonglong val = c->
locale().toLongLong(v, &ok);
44 if (Q_UNLIKELY(!ok)) {
48 "ValidatorBetween: Failed to parse value of field %s into number at %s::%s.",
50 qPrintable(c->controllerName()),
51 qPrintable(c->actionName()));
53 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
54 if (Q_UNLIKELY(!ok)) {
56 qCWarning(C_VALIDATOR,
57 "ValidatorBetween: Invalid minimum comparison value for field %s in "
60 qPrintable(c->controllerName()),
61 qPrintable(c->actionName()));
63 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
64 if (Q_UNLIKELY(!ok)) {
66 qCWarning(C_VALIDATOR,
67 "ValidatorBetween: Invalid maximum comparison value for field %s "
70 qPrintable(c->controllerName()),
71 qPrintable(c->actionName()));
73 if ((val < min) || (val > max)) {
76 QVariantMap{{QStringLiteral(
"val"), val},
77 {QStringLiteral(
"min"), min},
78 {QStringLiteral(
"max"), max}});
80 "ValidatorBetween: Validation failed for field %s in %s::%s: "
81 "%lli is not between %lli and %lli.",
83 qPrintable(c->controllerName()),
84 qPrintable(c->actionName()),
95 case QMetaType::UChar:
96 case QMetaType::UShort:
98 case QMetaType::ULong:
99 case QMetaType::ULongLong:
101 const qulonglong val = v.toULongLong(&ok);
102 if (Q_UNLIKELY(!ok)) {
106 "ValidatorBetween: Failed to parse value of field %s into number at %s::%s.",
108 qPrintable(c->controllerName()),
109 qPrintable(c->actionName()));
111 const qulonglong min = d->extractULongLong(c, params, d->min, &ok);
112 if (Q_UNLIKELY(!ok)) {
114 qCWarning(C_VALIDATOR,
115 "ValidatorBetween: Invalid minimum comparison value for field %s in "
118 qPrintable(c->controllerName()),
119 qPrintable(c->actionName()));
121 const qulonglong max = d->extractULongLong(c, params, d->max, &ok);
122 if (Q_UNLIKELY(!ok)) {
124 qCWarning(C_VALIDATOR,
125 "ValidatorBetween: Invalid maximum comparison value for field %s "
128 qPrintable(c->controllerName()),
129 qPrintable(c->actionName()));
131 if ((val < min) || (val > max)) {
134 QVariantMap{{QStringLiteral(
"val"), val},
135 {QStringLiteral(
"min"), min},
136 {QStringLiteral(
"max"), max}});
138 "ValidatorBetween: Validation failed for field %s in %s::%s: "
139 "%llu is not between %llu and %llu.",
141 qPrintable(c->controllerName()),
142 qPrintable(c->actionName()),
153 case QMetaType::Float:
154 case QMetaType::Double:
156 const double val = v.toDouble(&ok);
157 if (Q_UNLIKELY(!ok)) {
161 "ValidatorBetween: Failed to parse value of field %s into number at %s::%s.",
163 qPrintable(c->controllerName()),
164 qPrintable(c->actionName()));
166 const double min = d->extractDouble(c, params, d->min, &ok);
167 if (Q_UNLIKELY(!ok)) {
169 qCWarning(C_VALIDATOR,
170 "ValidatorBetween: Invalid minimum comparison value for field %s in "
173 qPrintable(c->controllerName()),
174 qPrintable(c->actionName()));
176 const double max = d->extractDouble(c, params, d->max, &ok);
177 if (Q_UNLIKELY(!ok)) {
179 qCWarning(C_VALIDATOR,
180 "ValidatorBetween: Invalid maximum comparison value for field %s "
183 qPrintable(c->controllerName()),
184 qPrintable(c->actionName()));
186 if ((val < min) || (val > max)) {
189 QVariantMap{{QStringLiteral(
"val"), val},
190 {QStringLiteral(
"min"), min},
191 {QStringLiteral(
"max"), max}});
193 "ValidatorBetween: Validation failed for field %s in %s::%s: "
194 "%f is not between %f and %f.",
196 qPrintable(c->controllerName()),
197 qPrintable(c->actionName()),
208 case QMetaType::QString:
210 const qlonglong val =
static_cast<qlonglong
>(v.length());
211 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
212 if (Q_UNLIKELY(!ok)) {
216 "ValidatorBetween: Invalid minimum comparison value for field %s in %s::%s.",
218 qPrintable(c->controllerName()),
219 qPrintable(c->actionName()));
221 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
222 if (Q_UNLIKELY(!ok)) {
224 qCWarning(C_VALIDATOR,
225 "ValidatorBetween: Invalid maximum comparison value for field %s in "
228 qPrintable(c->controllerName()),
229 qPrintable(c->actionName()));
231 if ((val < min) || (val > max)) {
234 QVariantMap{{QStringLiteral(
"val"), val},
235 {QStringLiteral(
"min"), min},
236 {QStringLiteral(
"max"), max}});
238 "ValidatorBetween: Validation failed for field %s in %s::%s: "
239 "string length %lli is not between %lli and %lli.",
241 qPrintable(c->controllerName()),
242 qPrintable(c->actionName()),
253 qCWarning(C_VALIDATOR,
254 "ValidatorBetween: The comparison type with ID %i for field %s at %s::%s is "
256 static_cast<int>(d->type),
258 qPrintable(c->controllerName()),
259 qPrintable(c->actionName()));
265 if (d->type != QMetaType::QString) {
266 const QVariant _v = d->valueToNumber(c, v, d->type);
273 result.
value.setValue(v);
284 const QVariant &errorData)
const
290 const QVariantMap map = errorData.toMap();
293 case QMetaType::Char:
294 case QMetaType::Short:
296 case QMetaType::Long:
297 case QMetaType::LongLong:
298 case QMetaType::QString:
299 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toLongLong());
300 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toLongLong());
302 case QMetaType::UChar:
303 case QMetaType::UShort:
304 case QMetaType::UInt:
305 case QMetaType::ULong:
306 case QMetaType::ULongLong:
307 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toULongLong());
308 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toULongLong());
310 case QMetaType::Float:
311 case QMetaType::Double:
312 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toDouble());
313 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toDouble());
320 const QString _label =
label(c);
322 if (_label.isEmpty()) {
323 if (d->type == QMetaType::QString) {
324 error = c->
translate(
"Cutelyst::ValidatorBetween",
325 "The text must be between %1 and %2 characters long.")
329 c->
translate(
"Cutelyst::ValidatorBetween",
"The value must be between %1 and %2.")
333 if (d->type == QMetaType::QString) {
335 "Cutelyst::ValidatorBetween",
336 "The text in the “%1“ field must be between %2 and %3 characters long.")
337 .arg(_label, min, max);
339 error = c->
translate(
"Cutelyst::ValidatorBetween",
340 "The value in the “%1” field must be between %2 and %3.")
341 .arg(_label, min, max);
352 int field = errorData.toInt();
353 const QString _label =
label(c);
356 if (_label.isEmpty()) {
357 error = c->
translate(
"Cutelyst::ValidatorBetween",
358 "The minimum comparison value is not valid.");
361 error = c->
translate(
"Cutelyst::ValidatorBetween",
362 "The minimum comparison value for the “%1” field is not valid.")
365 }
else if (
field == 0) {
367 if (_label.isEmpty()) {
368 error = c->
translate(
"Cutelyst::ValidatorBetween",
369 "The comparison type with ID %1 is not supported.")
370 .arg(
static_cast<int>(d->type));
374 c->
translate(
"Cutelyst::ValidatorBetween",
375 "The comparison type with ID %1 for the “%2” field is not supported.")
376 .arg(QString::number(
static_cast<int>(d->type)), _label);
378 }
else if (
field == 1) {
379 if (_label.isEmpty()) {
380 error = c->
translate(
"Cutelyst::ValidatorBetween",
381 "The maximum comparison value is not valid.");
384 error = c->
translate(
"Cutelyst::ValidatorBetween",
385 "The maximum comparison value for the “%1” field is not valid.")
399 const QString _label =
label(c);
400 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
401 if (_label.isEmpty()) {
402 error = c->
translate(
"Cutelyst::ValidatorBetween",
403 "Failed to parse the input value into a floating point number.");
406 error = c->
translate(
"Cutelyst::ValidatorBetween",
407 "Failed to parse the input value for the “%1” field into a "
408 "floating point number.")
412 if (_label.isEmpty()) {
413 error = c->
translate(
"Cutelyst::ValidatorBetween",
414 "Failed to parse the input value into an integer number.");
419 "Cutelyst::ValidatorBetween",
420 "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 or text length is between a minimum and maximum value.
ValidatorBetween(const QString &field, QMetaType::Type type, const QVariant &min, const QVariant &max, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new between validator.
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.
~ValidatorBetween() override
Deconstructs the between validator.
QString genericParsingError(Context *c, const QVariant &errorData) const override
Returns a generic error message for input value parsing errors.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error message for validation data 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.