6 #include "validatormax_p.h"
11 ValidatorRule(*new ValidatorMaxPrivate(field, type, max, messages, defValKey))
23 const QString v =
value(params);
32 case QMetaType::Short:
35 case QMetaType::LongLong:
37 const qlonglong val = c->
locale().toLongLong(v, &ok);
38 if (Q_UNLIKELY(!ok)) {
40 qCWarning(C_VALIDATOR,
"ValidatorMax: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
42 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
43 if (Q_UNLIKELY(!ok)) {
45 qCWarning(C_VALIDATOR,
"ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
49 {QStringLiteral(
"val"), val},
50 {QStringLiteral(
"max"), max}
52 qCDebug(C_VALIDATOR,
"ValidatorMax: Validation failed for field %s in %s::%s: %lli is not smaller than %lli.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, max);
60 case QMetaType::UChar:
61 case QMetaType::UShort:
63 case QMetaType::ULong:
64 case QMetaType::ULongLong:
66 const qulonglong val = v.toULongLong(&ok);
67 if (Q_UNLIKELY(!ok)) {
69 qCWarning(C_VALIDATOR,
"ValidatorMax: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
71 const qulonglong max = d->extractULongLong(c, params, d->max, &ok);
72 if (Q_UNLIKELY(!ok)) {
74 qCWarning(C_VALIDATOR,
"ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
78 {QStringLiteral(
"val"), val},
79 {QStringLiteral(
"max"), max}
81 qCDebug(C_VALIDATOR,
"ValidatorMax: Validation failed for field %s in %s::%s: %llu is not smaller than %llu.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, max);
89 case QMetaType::Float:
90 case QMetaType::Double:
92 const double val = v.toDouble(&ok);
93 if (Q_UNLIKELY(!ok)) {
95 qCWarning(C_VALIDATOR,
"ValidatorMax: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
97 const double max = d->extractDouble(c, params, d->max, &ok);
98 if (Q_UNLIKELY(!ok)) {
100 qCWarning(C_VALIDATOR,
"ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
104 {QStringLiteral(
"val"), val},
105 {QStringLiteral(
"max"), max}
107 qCDebug(C_VALIDATOR,
"ValidatorMax: Validation failed for field %s in %s::%s: %f is not smaller than %f.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, max);
115 case QMetaType::QString:
117 const qlonglong val =
static_cast<qlonglong
>(v.length());
118 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
119 if (Q_UNLIKELY(!ok)) {
121 qCWarning(C_VALIDATOR,
"ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
125 {QStringLiteral(
"val"), val},
126 {QStringLiteral(
"max"), max}
128 qCDebug(C_VALIDATOR,
"ValidatorMax: Validation failed for field %s in %s::%s: string length %lli is not smaller than %lli.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, max);
136 qCWarning(C_VALIDATOR,
"ValidatorMax: The comparison type with ID %i for field %s at %s::%s is not supported.",
static_cast<int>(d->type), qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
142 if (d->type != QMetaType::QString) {
143 const QVariant _v = d->valueToNumber(c, v, d->type);
150 result.
value.setValue(v);
166 const QVariantMap map = errorData.toMap();
169 case QMetaType::Char:
170 case QMetaType::Short:
172 case QMetaType::Long:
173 case QMetaType::LongLong:
174 case QMetaType::QString:
175 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toLongLong());
177 case QMetaType::UChar:
178 case QMetaType::UShort:
179 case QMetaType::UInt:
180 case QMetaType::ULong:
181 case QMetaType::ULongLong:
182 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toULongLong());
184 case QMetaType::Float:
185 case QMetaType::Double:
186 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toDouble());
193 const QString _label =
label(c);
195 if (_label.isEmpty()) {
196 if (d->type == QMetaType::QString) {
197 error = c->
translate(
"Cutelyst::ValidatorMax",
"The text must be shorter than %1 characters.").arg(max);
199 error = c->
translate(
"Cutelyst::ValidatorMax",
"The value must be lower than %1.").arg(max);
202 if (d->type == QMetaType::QString) {
203 error = c->
translate(
"Cutelyst::ValidatorMax",
"The text in the “%1“ field must be shorter than %2 characters.").arg(_label, max);
205 error = c->
translate(
"Cutelyst::ValidatorMax",
"The value in the “%1” field must be lower than %2.").arg(_label, max);
216 int field = errorData.toInt();
217 const QString _label =
label(c);
221 if (_label.isEmpty()) {
222 error = c->
translate(
"Cutelyst::ValidatorMax",
"The comparison type with ID %1 is not supported.").arg(
static_cast<int>(d->type));
224 error = c->
translate(
"Cutelyst::ValidatorMax",
"The comparison type with ID %1 for the “%2” field is not supported.").arg(QString::number(
static_cast<int>(d->type)), _label);
226 }
else if (
field == 1) {
227 if (_label.isEmpty()) {
228 error = c->
translate(
"Cutelyst::ValidatorMax",
"The maximum comparison value is not valid.");
230 error = c->
translate(
"Cutelyst::ValidatorMax",
"The maximum comparison value for the “%1” field is not valid.").arg(_label);
243 const QString _label =
label(c);
244 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
245 if (_label.isEmpty()) {
246 error = c->
translate(
"Cutelyst::ValidatorMax",
"Failed to parse the input value into a floating point number.");
248 error = c->
translate(
"Cutelyst::ValidatorMax",
"Failed to parse the input value for the “%1” field into a floating point number.").arg(_label);
251 if (_label.isEmpty()) {
252 error = c->
translate(
"Cutelyst::ValidatorMax",
"Failed to parse the input value into an integer number.");
254 error = c->
translate(
"Cutelyst::ValidatorMax",
"Failed to parse the input value for the “%1” field into an integer number.").arg(_label);
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 occured 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.