6#include "validatormin_p.h"
14 const QString &defValKey)
15 :
ValidatorRule(*new ValidatorMinPrivate(field, type, min, messages, defValKey))
27 const QString v =
value(params);
37 case QMetaType::Short:
40 case QMetaType::LongLong:
42 const qlonglong val = c->
locale().toLongLong(v, &ok);
43 if (Q_UNLIKELY(!ok)) {
45 qCWarning(C_VALIDATOR,
46 "ValidatorMin: Failed to parse value of field %s into number at %s::%s.",
48 qPrintable(c->controllerName()),
49 qPrintable(c->actionName()));
51 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
52 if (Q_UNLIKELY(!ok)) {
56 "ValidatorMin: Invalid minimum comparison value for field %s in %s::%s.",
58 qPrintable(c->controllerName()),
59 qPrintable(c->actionName()));
64 QVariantMap{{QStringLiteral(
"val"), val},
65 {QStringLiteral(
"min"), min}});
67 "ValidatorMin: Validation failed for field %s in %s::%s: %lli is "
68 "not greater than %lli.",
70 qPrintable(c->controllerName()),
71 qPrintable(c->actionName()),
80 case QMetaType::UChar:
81 case QMetaType::UShort:
83 case QMetaType::ULong:
84 case QMetaType::ULongLong:
86 const qulonglong val = v.toULongLong(&ok);
87 if (Q_UNLIKELY(!ok)) {
89 qCWarning(C_VALIDATOR,
90 "ValidatorMin: Failed to parse value of field %s into number at %s::%s.",
92 qPrintable(c->controllerName()),
93 qPrintable(c->actionName()));
95 const qulonglong min = d->extractULongLong(c, params, d->min, &ok);
96 if (Q_UNLIKELY(!ok)) {
100 "ValidatorMin: Invalid minimum comparison value for field %s in %s::%s.",
102 qPrintable(c->controllerName()),
103 qPrintable(c->actionName()));
108 QVariantMap{{QStringLiteral(
"val"), val},
109 {QStringLiteral(
"min"), min}});
111 "ValidatorMin: Validation failed for field %s in %s::%s: %llu is "
112 "not greater than %llu.",
114 qPrintable(c->controllerName()),
115 qPrintable(c->actionName()),
124 case QMetaType::Float:
125 case QMetaType::Double:
127 const double val = v.toDouble(&ok);
128 if (Q_UNLIKELY(!ok)) {
130 qCWarning(C_VALIDATOR,
131 "ValidatorMin: Failed to parse value of field %s into number at %s::%s.",
133 qPrintable(c->controllerName()),
134 qPrintable(c->actionName()));
136 const double min = d->extractDouble(c, params, d->min, &ok);
137 if (Q_UNLIKELY(!ok)) {
141 "ValidatorMin: Invalid minimum comparison value for field %s in %s::%s.",
143 qPrintable(c->controllerName()),
144 qPrintable(c->actionName()));
149 QVariantMap{{QStringLiteral(
"val"), val},
150 {QStringLiteral(
"min"), min}});
152 "ValidatorMin: Validation failed for field %s in %s::%s: %f is not "
155 qPrintable(c->controllerName()),
156 qPrintable(c->actionName()),
165 case QMetaType::QString:
167 const qlonglong val =
static_cast<qlonglong
>(v.length());
168 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
169 if (Q_UNLIKELY(!ok)) {
171 qCWarning(C_VALIDATOR,
172 "ValidatorMin: Invalid minimum comparison value for field %s in %s::%s.",
174 qPrintable(c->controllerName()),
175 qPrintable(c->actionName()));
179 c, QVariantMap{{QStringLiteral(
"val"), val}, {QStringLiteral(
"min"), min}});
181 "ValidatorMin: Validation failed for field %s in %s::%s: string length "
182 "%lli is not longer than %lli.",
184 qPrintable(c->controllerName()),
185 qPrintable(c->actionName()),
194 qCWarning(C_VALIDATOR,
195 "ValidatorMin: The comparison type with ID %i for field %s at %s::%s is not "
197 static_cast<int>(d->type),
199 qPrintable(c->controllerName()),
200 qPrintable(c->actionName()));
206 if (d->type != QMetaType::QString) {
207 const QVariant _v = d->valueToNumber(c, v, d->type);
214 result.
value.setValue(v);
230 const QVariantMap map = errorData.toMap();
233 case QMetaType::Char:
234 case QMetaType::Short:
236 case QMetaType::Long:
237 case QMetaType::LongLong:
238 case QMetaType::QString:
239 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toLongLong());
241 case QMetaType::UChar:
242 case QMetaType::UShort:
243 case QMetaType::UInt:
244 case QMetaType::ULong:
245 case QMetaType::ULongLong:
246 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toULongLong());
248 case QMetaType::Float:
249 case QMetaType::Double:
250 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toDouble());
257 const QString _label =
label(c);
259 if (_label.isEmpty()) {
260 if (d->type == QMetaType::QString) {
261 error = c->
translate(
"Cutelyst::ValidatorMin",
262 "The text must be longer than %1 characters.")
265 error = c->
translate(
"Cutelyst::ValidatorMin",
"The value must be greater than %1.")
269 if (d->type == QMetaType::QString) {
270 error = c->
translate(
"Cutelyst::ValidatorMin",
271 "The text in the “%1“ field must be longer than %2 characters.")
274 error = c->
translate(
"Cutelyst::ValidatorMin",
275 "The value in the “%1” field must be greater than %2.")
287 int field = errorData.toInt();
288 const QString _label =
label(c);
291 if (_label.isEmpty()) {
292 error = c->
translate(
"Cutelyst::ValidatorMin",
293 "The minimum comparison value is not valid.");
295 error = c->
translate(
"Cutelyst::ValidatorMin",
296 "The minimum comparison value for the “%1” field is not valid.")
299 }
else if (
field == 0) {
301 if (_label.isEmpty()) {
302 error = c->
translate(
"Cutelyst::ValidatorMin",
303 "The comparison type with ID %1 is not supported.")
304 .arg(
static_cast<int>(d->type));
308 "The comparison type with ID %1 for the “%2” field is not supported.")
309 .arg(QString::number(
static_cast<int>(d->type)), _label);
322 const QString _label =
label(c);
323 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
324 if (_label.isEmpty()) {
325 error = c->
translate(
"Cutelyst::ValidatorMin",
326 "Failed to parse the input value into a floating point number.");
328 error = c->
translate(
"Cutelyst::ValidatorMin",
329 "Failed to parse the input value for the “%1” field into a "
330 "floating point number.")
334 if (_label.isEmpty()) {
335 error = c->
translate(
"Cutelyst::ValidatorMin",
336 "Failed to parse the input value into an integer number.");
340 "Cutelyst::ValidatorMin",
341 "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 smaller or shorter than a maximum value.
~ValidatorMin() override
Deconstructs the min 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.
ValidatorMin(const QString &field, QMetaType::Type type, const QVariant &min, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new min validator.
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.