6 #include "validatorbetween_p.h" 16 :
ValidatorRule(*new ValidatorBetweenPrivate(field, type, min, max, messages, defValKey))
44 if (Q_UNLIKELY(!ok)) {
48 "ValidatorBetween: Failed to parse value of field %s into number at %s::%s.",
50 qPrintable(c->controllerName()),
51 qPrintable(c->actionName()));
53 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
54 if (Q_UNLIKELY(!ok)) {
56 qCWarning(C_VALIDATOR,
57 "ValidatorBetween: Invalid minimum comparison value for field %s in " 60 qPrintable(c->controllerName()),
61 qPrintable(c->actionName()));
63 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
64 if (Q_UNLIKELY(!ok)) {
66 qCWarning(C_VALIDATOR,
67 "ValidatorBetween: Invalid maximum comparison value for field %s " 70 qPrintable(c->controllerName()),
71 qPrintable(c->actionName()));
73 if ((val < min) || (val > max)) {
76 QVariantMap{{QStringLiteral(
"val"), val},
77 {QStringLiteral(
"min"), min},
78 {QStringLiteral(
"max"), max}});
80 "ValidatorBetween: Validation failed for field %s in %s::%s: " 81 "%lli is not between %lli and %lli.",
83 qPrintable(c->controllerName()),
84 qPrintable(c->actionName()),
102 if (Q_UNLIKELY(!ok)) {
106 "ValidatorBetween: Failed to parse value of field %s into number at %s::%s.",
108 qPrintable(c->controllerName()),
109 qPrintable(c->actionName()));
111 const qulonglong min = d->extractULongLong(c, params, d->min, &ok);
112 if (Q_UNLIKELY(!ok)) {
114 qCWarning(C_VALIDATOR,
115 "ValidatorBetween: Invalid minimum comparison value for field %s in " 118 qPrintable(c->controllerName()),
119 qPrintable(c->actionName()));
121 const qulonglong max = d->extractULongLong(c, params, d->max, &ok);
122 if (Q_UNLIKELY(!ok)) {
124 qCWarning(C_VALIDATOR,
125 "ValidatorBetween: Invalid maximum comparison value for field %s " 128 qPrintable(c->controllerName()),
129 qPrintable(c->actionName()));
131 if ((val < min) || (val > max)) {
134 QVariantMap{{QStringLiteral(
"val"), val},
135 {QStringLiteral(
"min"), min},
136 {QStringLiteral(
"max"), max}});
138 "ValidatorBetween: Validation failed for field %s in %s::%s: " 139 "%llu is not between %llu and %llu.",
141 qPrintable(c->controllerName()),
142 qPrintable(c->actionName()),
157 if (Q_UNLIKELY(!ok)) {
161 "ValidatorBetween: Failed to parse value of field %s into number at %s::%s.",
163 qPrintable(c->controllerName()),
164 qPrintable(c->actionName()));
166 const double min = d->extractDouble(c, params, d->min, &ok);
167 if (Q_UNLIKELY(!ok)) {
169 qCWarning(C_VALIDATOR,
170 "ValidatorBetween: Invalid minimum comparison value for field %s in " 173 qPrintable(c->controllerName()),
174 qPrintable(c->actionName()));
176 const double max = d->extractDouble(c, params, d->max, &ok);
177 if (Q_UNLIKELY(!ok)) {
179 qCWarning(C_VALIDATOR,
180 "ValidatorBetween: Invalid maximum comparison value for field %s " 183 qPrintable(c->controllerName()),
184 qPrintable(c->actionName()));
186 if ((val < min) || (val > max)) {
189 QVariantMap{{QStringLiteral(
"val"), val},
190 {QStringLiteral(
"min"), min},
191 {QStringLiteral(
"max"), max}});
193 "ValidatorBetween: Validation failed for field %s in %s::%s: " 194 "%f is not between %f and %f.",
196 qPrintable(c->controllerName()),
197 qPrintable(c->actionName()),
210 const qlonglong val =
static_cast<qlonglong
>(v.
length());
211 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
212 if (Q_UNLIKELY(!ok)) {
216 "ValidatorBetween: Invalid minimum comparison value for field %s in %s::%s.",
218 qPrintable(c->controllerName()),
219 qPrintable(c->actionName()));
221 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
222 if (Q_UNLIKELY(!ok)) {
224 qCWarning(C_VALIDATOR,
225 "ValidatorBetween: Invalid maximum comparison value for field %s in " 228 qPrintable(c->controllerName()),
229 qPrintable(c->actionName()));
231 if ((val < min) || (val > max)) {
234 QVariantMap{{QStringLiteral(
"val"), val},
235 {QStringLiteral(
"min"), min},
236 {QStringLiteral(
"max"), max}});
238 "ValidatorBetween: Validation failed for field %s in %s::%s: " 239 "string length %lli is not between %lli and %lli.",
241 qPrintable(c->controllerName()),
242 qPrintable(c->actionName()),
253 qCWarning(C_VALIDATOR,
254 "ValidatorBetween: The comparison type with ID %i for field %s at %s::%s is " 256 static_cast<int>(d->type),
258 qPrintable(c->controllerName()),
259 qPrintable(c->actionName()));
266 const QVariant _v = d->valueToNumber(c, v, d->type);
290 const QVariantMap map = errorData.
toMap();
299 min = c->
locale().
toString(map.value(QStringLiteral(
"min")).toLongLong());
300 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toLongLong());
307 min = c->
locale().
toString(map.value(QStringLiteral(
"min")).toULongLong());
308 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toULongLong());
312 min = c->
locale().
toString(map.value(QStringLiteral(
"min")).toDouble());
313 max = c->
locale().
toString(map.value(QStringLiteral(
"max")).toDouble());
324 error = c->
translate(
"Cutelyst::ValidatorBetween",
325 "The text must be between %1 and %2 characters long.")
329 c->
translate(
"Cutelyst::ValidatorBetween",
"The value must be between %1 and %2.")
335 "Cutelyst::ValidatorBetween",
336 "The text in the “%1“ field must be between %2 and %3 characters long.")
337 .
arg(_label, min, max);
339 error = c->
translate(
"Cutelyst::ValidatorBetween",
340 "The value in the “%1” field must be between %2 and %3.")
341 .
arg(_label, min, max);
357 error = c->
translate(
"Cutelyst::ValidatorBetween",
358 "The minimum comparison value is not valid.");
361 error = c->
translate(
"Cutelyst::ValidatorBetween",
362 "The minimum comparison value for the “%1” field is not valid.")
365 }
else if (
field == 0) {
368 error = c->
translate(
"Cutelyst::ValidatorBetween",
369 "The comparison type with ID %1 is not supported.")
370 .
arg(static_cast<int>(d->type));
374 c->
translate(
"Cutelyst::ValidatorBetween",
375 "The comparison type with ID %1 for the “%2” field is not supported.")
378 }
else if (
field == 1) {
380 error = c->
translate(
"Cutelyst::ValidatorBetween",
381 "The maximum comparison value is not valid.");
384 error = c->
translate(
"Cutelyst::ValidatorBetween",
385 "The maximum comparison value for the “%1” field is not valid.")
402 error = c->
translate(
"Cutelyst::ValidatorBetween",
403 "Failed to parse the input value into a floating point number.");
406 error = c->
translate(
"Cutelyst::ValidatorBetween",
407 "Failed to parse the input value for the “%1” field into a " 408 "floating point number.")
413 error = c->
translate(
"Cutelyst::ValidatorBetween",
414 "Failed to parse the input value into an integer number.");
419 "Cutelyst::ValidatorBetween",
420 "Failed to parse the input value for the “%1” field into an integer number.")
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occurred while parsing input.
Checks if a value or text length is between a minimum and maximum value.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
QString toString(qlonglong i) const const
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
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 const
QString number(int n, int base)
~ValidatorBetween() override
Deconstructs the between validator.
int toInt(bool *ok) const const
QString genericParsingError(Context *c, const QVariant &errorData) const override
Returns a generic error message for input value parsing errors.
qlonglong toLongLong(const QString &s, bool *ok) const const
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error message for validation data errors.
The Cutelyst namespace holds all public Cutelyst API.
Base class for all validator rules.
qulonglong toULongLong(bool *ok, int base) const const
QLocale locale() const noexcept
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
void setValue(const T &value)
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
QMap< QString, QVariant > toMap() const 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 arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString field() const
Returns the name of the field to validate.
bool isValid() const const
Contains the result of a single input parameter validation.
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 ...