19 #include "validatormax_p.h"
21 using namespace Cutelyst;
24 ValidatorRule(*new ValidatorMaxPrivate(field, type, max, messages, defValKey))
45 case QMetaType::Short:
48 case QMetaType::LongLong:
51 if (Q_UNLIKELY(!ok)) {
53 qCWarning(C_VALIDATOR,
"ValidatorMax: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
55 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
56 if (Q_UNLIKELY(!ok)) {
58 qCWarning(C_VALIDATOR,
"ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
62 {QStringLiteral(
"val"), val},
63 {QStringLiteral(
"max"), max}
65 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);
73 case QMetaType::UChar:
74 case QMetaType::UShort:
76 case QMetaType::ULong:
77 case QMetaType::ULongLong:
80 if (Q_UNLIKELY(!ok)) {
82 qCWarning(C_VALIDATOR,
"ValidatorMax: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
84 const qulonglong max = d->extractULongLong(c, params, d->max, &ok);
85 if (Q_UNLIKELY(!ok)) {
87 qCWarning(C_VALIDATOR,
"ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
91 {QStringLiteral(
"val"), val},
92 {QStringLiteral(
"max"), max}
94 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);
102 case QMetaType::Float:
103 case QMetaType::Double:
106 if (Q_UNLIKELY(!ok)) {
108 qCWarning(C_VALIDATOR,
"ValidatorMax: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
110 const double max = d->extractDouble(c, params, d->max, &ok);
111 if (Q_UNLIKELY(!ok)) {
113 qCWarning(C_VALIDATOR,
"ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
117 {QStringLiteral(
"val"), val},
118 {QStringLiteral(
"max"), max}
120 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);
128 case QMetaType::QString:
130 const qlonglong val =
static_cast<qlonglong
>(v.
length());
131 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
132 if (Q_UNLIKELY(!ok)) {
134 qCWarning(C_VALIDATOR,
"ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
138 {QStringLiteral(
"val"), val},
139 {QStringLiteral(
"max"), max}
141 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);
149 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()));
155 if (d->type != QMetaType::QString) {
156 const QVariant _v = d->valueToNumber(c, v, d->type);
179 const QVariantMap map = errorData.
toMap();
182 case QMetaType::Char:
183 case QMetaType::Short:
185 case QMetaType::Long:
186 case QMetaType::LongLong:
187 case QMetaType::QString:
188 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toLongLong());
190 case QMetaType::UChar:
191 case QMetaType::UShort:
192 case QMetaType::UInt:
193 case QMetaType::ULong:
194 case QMetaType::ULongLong:
195 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toULongLong());
197 case QMetaType::Float:
198 case QMetaType::Double:
199 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toDouble());
209 if (d->type == QMetaType::QString) {
210 error = c->
translate(
"Cutelyst::ValidatorMax",
"The text must be shorter than %1 characters.").
arg(max);
212 error = 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",
"The text in the “%1“ field must be shorter than %2 characters.").
arg(_label, max);
218 error = c->
translate(
"Cutelyst::ValidatorMax",
"The value in the “%1” field must be lower than %2.").
arg(_label, max);
235 error = c->
translate(
"Cutelyst::ValidatorMax",
"The comparison type with ID %1 is not supported.").
arg(static_cast<int>(d->type));
237 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);
239 }
else if (field == 1) {
241 error = c->
translate(
"Cutelyst::ValidatorMax",
"The maximum comparison value is not valid.");
243 error = c->
translate(
"Cutelyst::ValidatorMax",
"The maximum comparison value for the “%1” field is not valid.").
arg(_label);
257 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
259 error = c->
translate(
"Cutelyst::ValidatorMax",
"Failed to parse the input value into a floating point number.");
261 error = c->
translate(
"Cutelyst::ValidatorMax",
"Failed to parse the input value for the “%1” field into a floating point number.").
arg(_label);
265 error = c->
translate(
"Cutelyst::ValidatorMax",
"Failed to parse the input value into an integer number.");
267 error = c->
translate(
"Cutelyst::ValidatorMax",
"Failed to parse the input value for the “%1” field into an integer number.").
arg(_label);
QString field() const
Returns the name of the field to validate.
QString toString(qlonglong i) const
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
Stores custom error messages and the input field label.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error message for validation data errors.
double toDouble(bool *ok) const
QString number(int n, int base)
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
int toInt(bool *ok) const
QString genericParsingError(Context *c, const QVariant &errorData) const override
Returns a generic error message for input value parsing errors.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
qlonglong toLongLong(const QString &s, bool *ok) const
ValidatorMax(const QString &field, QMetaType::Type type, const QVariant &max, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new max validator.
Base class for all validator rules.
qulonglong toULongLong(bool *ok, int base) const
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 ...
void setValue(const T &value)
QMap< QString, QVariant > toMap() const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
~ValidatorMax() override
Deconstructs the max validator.
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
Checks if a value is not bigger or longer than a maximum value.
Contains the result of a single input parameter validation.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occured while parsing input.