19 #include "validatormax_p.h"
24 ValidatorRule(*new ValidatorMaxPrivate(field, type, max, messages, defValKey))
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);
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);
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);
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()));
156 const QVariant _v = d->valueToNumber(c, v, d->type);
179 const QVariantMap map = errorData.
toMap();
188 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toLongLong());
195 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toULongLong());
199 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toDouble());
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);
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);
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);
bool isValid() const const
Stores custom error messages and the input field label.
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
QString number(int n, int base)
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error message for validation data errors.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Base class for all validator rules.
void setValue(const T &value)
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occured while parsing input.
qulonglong toULongLong(bool *ok, int base) const const
QString field() const
Returns the name of the field to validate.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
bool isEmpty() const const
Contains the result of a single input parameter validation.
QString toString(qlonglong i) const const
~ValidatorMax() override
Deconstructs the max validator.
int toInt(bool *ok) const const
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message.
double toDouble(bool *ok) const const
QMap< QString, QVariant > toMap() const const
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
The Cutelyst namespace holds all public Cutelyst API.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
ValidatorMax(const QString &field, QMetaType::Type type, const QVariant &max, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new max validator.
qlonglong toLongLong(const QString &s, bool *ok) const const
Checks if a value is not bigger or longer than a maximum value.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString genericParsingError(Context *c, const QVariant &errorData) const override
Returns a generic error message for input value parsing errors.
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 ...