19 #include "validatorbetween_p.h"
21 using namespace Cutelyst;
24 ValidatorRule(*new ValidatorBetweenPrivate(field, type, min, max, messages, defValKey))
46 case QMetaType::Short:
49 case QMetaType::LongLong:
52 if (Q_UNLIKELY(!ok)) {
54 qCWarning(C_VALIDATOR,
"ValidatorBetween: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
56 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
57 if (Q_UNLIKELY(!ok)) {
59 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid minimum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
61 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
62 if (Q_UNLIKELY(!ok)) {
64 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
66 if ((val < min) || (val > max)) {
68 {QStringLiteral(
"val"), val},
69 {QStringLiteral(
"min"), min},
70 {QStringLiteral(
"max"), max}
72 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);
81 case QMetaType::UChar:
82 case QMetaType::UShort:
84 case QMetaType::ULong:
85 case QMetaType::ULongLong:
88 if (Q_UNLIKELY(!ok)) {
90 qCWarning(C_VALIDATOR,
"ValidatorBetween: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
92 const qulonglong min = d->extractULongLong(c, params, d->min, &ok);
93 if (Q_UNLIKELY(!ok)) {
95 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid minimum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
97 const qulonglong max = d->extractULongLong(c, params, d->max, &ok);
98 if (Q_UNLIKELY(!ok)) {
100 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
102 if ((val < min) || (val > max)) {
104 {QStringLiteral(
"val"), val},
105 {QStringLiteral(
"min"), min},
106 {QStringLiteral(
"max"), max}
108 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);
117 case QMetaType::Float:
118 case QMetaType::Double:
121 if (Q_UNLIKELY(!ok)) {
123 qCWarning(C_VALIDATOR,
"ValidatorBetween: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
125 const double min = d->extractDouble(c, params, d->min, &ok);
126 if (Q_UNLIKELY(!ok)) {
128 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid minimum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
130 const double max = d->extractDouble(c, params, d->max, &ok);
131 if (Q_UNLIKELY(!ok)) {
133 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
135 if ((val < min) || (val > max)) {
137 {QStringLiteral(
"val"), val},
138 {QStringLiteral(
"min"), min},
139 {QStringLiteral(
"max"), max}
141 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);
150 case QMetaType::QString:
152 const qlonglong val =
static_cast<qlonglong
>(v.
length());
153 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
154 if (Q_UNLIKELY(!ok)) {
156 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid minimum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
158 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
159 if (Q_UNLIKELY(!ok)) {
161 qCWarning(C_VALIDATOR,
"ValidatorBetween: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
163 if ((val < min) || (val > max)) {
165 {QStringLiteral(
"val"), val},
166 {QStringLiteral(
"min"), min},
167 {QStringLiteral(
"max"), max}
169 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);
178 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()));
184 if (d->type != QMetaType::QString) {
185 const QVariant _v = d->valueToNumber(c, v, d->type);
208 const QVariantMap map = errorData.
toMap();
211 case QMetaType::Char:
212 case QMetaType::Short:
214 case QMetaType::Long:
215 case QMetaType::LongLong:
216 case QMetaType::QString:
217 min = c->
locale().
toString(map.value(QStringLiteral(
"min")).toLongLong());
218 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toLongLong());
220 case QMetaType::UChar:
221 case QMetaType::UShort:
222 case QMetaType::UInt:
223 case QMetaType::ULong:
224 case QMetaType::ULongLong:
225 min = c->
locale().
toString(map.value(QStringLiteral(
"min")).toULongLong());
226 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toULongLong());
228 case QMetaType::Float:
229 case QMetaType::Double:
230 min = c->
locale().
toString(map.value(QStringLiteral(
"min")).toDouble());
231 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toDouble());
241 if (d->type == QMetaType::QString) {
242 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The text must be between %1 and %2 characters long.").
arg(min, max);
244 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The value must be between %1 and %2.").
arg(min, max);
247 if (d->type == QMetaType::QString) {
248 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The text in the “%1“ field must be between %2 and %3 characters long.").
arg(_label, min, max);
250 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The value in the “%1” field must be between %2 and %3.").
arg(_label, min, max);
266 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The minimum comparison value is not valid.");
269 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The minimum comparison value for the “%1” field is not valid.").
arg(_label);
271 }
else if (field == 0) {
274 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The comparison type with ID %1 is not supported.").
arg(static_cast<int>(d->type));
277 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);
279 }
else if (field == 1) {
281 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The maximum comparison value is not valid.");
284 error = c->
translate(
"Cutelyst::ValidatorBetween",
"The maximum comparison value for the “%1” field is not valid.").
arg(_label);
298 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
300 error = c->
translate(
"Cutelyst::ValidatorBetween",
"Failed to parse the input value into a floating point number.");
303 error = c->
translate(
"Cutelyst::ValidatorBetween",
"Failed to parse the input value for the “%1” field into a floating point number.").
arg(_label);
307 error = c->
translate(
"Cutelyst::ValidatorBetween",
"Failed to parse the input value into an integer number.");
310 error = c->
translate(
"Cutelyst::ValidatorBetween",
"Failed to parse the input value for the “%1” field into an integer number.").
arg(_label);
Checks if a value or text length is between a minimum and maximum value.
QString field() const
Returns the name of the field to validate.
QString toString(qlonglong i) const
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
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 genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message.
double toDouble(bool *ok) const
QString number(int n, int base)
~ValidatorBetween() override
Deconstructs the between validator.
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
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error message for validation data errors.
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
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 translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
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 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.