6 #include "validatorbetween_p.h"
11 ValidatorRule(*new ValidatorBetweenPrivate(field, type, min, max, messages, defValKey))
39 if (Q_UNLIKELY(!ok)) {
41 qCWarning(C_VALIDATOR,
"ValidatorBetween: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
43 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
44 if (Q_UNLIKELY(!ok)) {
46 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid minimum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
48 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
49 if (Q_UNLIKELY(!ok)) {
51 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
53 if ((val < min) || (val > max)) {
55 {QStringLiteral(
"val"), val},
56 {QStringLiteral(
"min"), min},
57 {QStringLiteral(
"max"), max}
59 qCDebug(C_VALIDATOR,
"ValidatorBetween: Validation failed for field %s in %s::%s: %lli is not between %lli and %lli.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, min, max);
75 if (Q_UNLIKELY(!ok)) {
77 qCWarning(C_VALIDATOR,
"ValidatorBetween: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
79 const qulonglong min = d->extractULongLong(c, params, d->min, &ok);
80 if (Q_UNLIKELY(!ok)) {
82 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid minimum comparison value for field %s in %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,
"ValidatorBetween: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
89 if ((val < min) || (val > max)) {
91 {QStringLiteral(
"val"), val},
92 {QStringLiteral(
"min"), min},
93 {QStringLiteral(
"max"), max}
95 qCDebug(C_VALIDATOR,
"ValidatorBetween: Validation failed for field %s in %s::%s: %llu is not between %llu and %llu.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, min, max);
108 if (Q_UNLIKELY(!ok)) {
110 qCWarning(C_VALIDATOR,
"ValidatorBetween: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
112 const double min = d->extractDouble(c, params, d->min, &ok);
113 if (Q_UNLIKELY(!ok)) {
115 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid minimum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
117 const double max = d->extractDouble(c, params, d->max, &ok);
118 if (Q_UNLIKELY(!ok)) {
120 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
122 if ((val < min) || (val > max)) {
124 {QStringLiteral(
"val"), val},
125 {QStringLiteral(
"min"), min},
126 {QStringLiteral(
"max"), max}
128 qCDebug(C_VALIDATOR,
"ValidatorBetween: Validation failed for field %s in %s::%s: %f is not between %f and %f.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, min, max);
139 const qlonglong val =
static_cast<qlonglong
>(v.
length());
140 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
141 if (Q_UNLIKELY(!ok)) {
143 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid minimum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
145 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
146 if (Q_UNLIKELY(!ok)) {
148 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
150 if ((val < min) || (val > max)) {
152 {QStringLiteral(
"val"), val},
153 {QStringLiteral(
"min"), min},
154 {QStringLiteral(
"max"), max}
156 qCDebug(C_VALIDATOR,
"ValidatorBetween: Validation failed for field %s in %s::%s: string length %lli is not between %lli and %lli.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, min, max);
165 qCWarning(C_VALIDATOR,
"ValidatorBetween: 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()));
172 const QVariant _v = d->valueToNumber(c, v, d->type);
195 const QVariantMap map = errorData.
toMap();
204 min = c->
locale().
toString(map.value(QStringLiteral(
"min")).toLongLong());
205 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toLongLong());
212 min = c->
locale().
toString(map.value(QStringLiteral(
"min")).toULongLong());
213 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toULongLong());
217 min = c->
locale().
toString(map.value(QStringLiteral(
"min")).toDouble());
218 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toDouble());
229 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The text must be between %1 and %2 characters long.").
arg(min, max);
231 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The value must be between %1 and %2.").
arg(min, max);
235 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The text in the “%1“ field must be between %2 and %3 characters long.").
arg(_label, min, max);
237 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The value in the “%1” field must be between %2 and %3.").
arg(_label, min, max);
253 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The minimum comparison value is not valid.");
256 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The minimum comparison value for the “%1” field is not valid.").
arg(_label);
258 }
else if (
field == 0) {
261 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The comparison type with ID %1 is not supported.").
arg(
static_cast<int>(d->type));
264 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The comparison type with ID %1 for the “%2” field is not supported.").
arg(
QString::number(
static_cast<int>(d->type)), _label);
266 }
else if (
field == 1) {
268 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The maximum comparison value is not valid.");
271 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The maximum comparison value for the “%1” field is not valid.").
arg(_label);
287 error = c->
translate(
"Cutelyst::ValidatorBetween",
"Failed to parse the input value into a floating point number.");
290 error = c->
translate(
"Cutelyst::ValidatorBetween",
"Failed to parse the input value for the “%1” field into a floating point number.").
arg(_label);
294 error = c->
translate(
"Cutelyst::ValidatorBetween",
"Failed to parse the input value into an integer number.");
297 error = c->
translate(
"Cutelyst::ValidatorBetween",
"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 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.
qlonglong toLongLong(const QString &s, bool *ok) const const
QString toString(qlonglong i) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
QString number(int n, int base)
double toDouble(bool *ok) const const
qulonglong toULongLong(bool *ok, int base) const const
bool isValid() const const
void setValue(const T &value)
int toInt(bool *ok) const const
QMap< QString, QVariant > toMap() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.