6#include "validatorbetween_p.h"
15 const QString &defValKey)
16 :
ValidatorRule(*new ValidatorBetweenPrivate(field, type, min, max, messages, defValKey))
26 const QString v =
value(params);
36 case QMetaType::Short:
39 case QMetaType::LongLong:
41 const qlonglong val = c->
locale().toLongLong(v, &ok);
42 if (Q_UNLIKELY(!ok)) {
44 qCWarning(C_VALIDATOR).noquote().nospace()
46 <<
"\" into an integer number";
48 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
49 if (Q_UNLIKELY(!ok)) {
51 qCWarning(C_VALIDATOR).noquote()
52 <<
"Invalid mininum comparison value:" << d->min;
54 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
55 if (Q_UNLIKELY(!ok)) {
57 qCWarning(C_VALIDATOR).noquote()
58 <<
"Invalid maximum comparison value:" << d->max;
60 if ((val < min) || (val > max)) {
63 QVariantMap{{QStringLiteral(
"val"), val},
64 {QStringLiteral(
"min"), min},
65 {QStringLiteral(
"max"), max}});
66 qCDebug(C_VALIDATOR).noquote()
67 <<
debugString(c) << val <<
"is not between" << min <<
"and" << max;
75 case QMetaType::UChar:
76 case QMetaType::UShort:
78 case QMetaType::ULong:
79 case QMetaType::ULongLong:
81 const qulonglong val = v.toULongLong(&ok);
82 if (Q_UNLIKELY(!ok)) {
84 qCWarning(C_VALIDATOR).noquote().nospace()
86 <<
"\" into an unsigned integer number";
88 const qulonglong min = d->extractULongLong(c, params, d->min, &ok);
89 if (Q_UNLIKELY(!ok)) {
91 qCWarning(C_VALIDATOR).noquote()
92 <<
debugString(c) <<
"Invalid mininum comparison value:" << d->min;
94 const qulonglong max = d->extractULongLong(c, params, d->max, &ok);
95 if (Q_UNLIKELY(!ok)) {
97 qCWarning(C_VALIDATOR).noquote()
98 <<
debugString(c) <<
"Invalid maximum comparison value:" << d->max;
100 if ((val < min) || (val > max)) {
103 QVariantMap{{QStringLiteral(
"val"), val},
104 {QStringLiteral(
"min"), min},
105 {QStringLiteral(
"max"), max}});
106 qCDebug(C_VALIDATOR).noquote()
107 <<
debugString(c) << val <<
"is not between" << min <<
"and" << max;
115 case QMetaType::Float:
116 case QMetaType::Double:
118 const double val = v.toDouble(&ok);
119 if (Q_UNLIKELY(!ok)) {
121 qCWarning(C_VALIDATOR).noquote().nospace()
122 <<
debugString(c) <<
" Can not parse input \"" << v
123 <<
"\" into a floating point number";
125 const double min = d->extractDouble(c, params, d->min, &ok);
126 if (Q_UNLIKELY(!ok)) {
128 qCWarning(C_VALIDATOR).noquote()
129 <<
debugString(c) <<
"Invalid mininum comparison value:" << d->min;
131 const double max = d->extractDouble(c, params, d->max, &ok);
132 if (Q_UNLIKELY(!ok)) {
134 qCWarning(C_VALIDATOR).noquote()
135 <<
debugString(c) <<
"Invalid maximum comparison value:" << d->max;
137 if ((val < min) || (val > max)) {
140 QVariantMap{{QStringLiteral(
"val"), val},
141 {QStringLiteral(
"min"), min},
142 {QStringLiteral(
"max"), max}});
143 qCDebug(C_VALIDATOR).noquote()
144 <<
debugString(c) << val <<
"is not between" << min <<
"and" << max;
152 case QMetaType::QString:
154 const auto val =
static_cast<qlonglong
>(v.length());
155 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
156 if (Q_UNLIKELY(!ok)) {
158 qCWarning(C_VALIDATOR).noquote()
159 <<
debugString(c) <<
"Invalid mininum comparison value:" << d->min;
161 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
162 if (Q_UNLIKELY(!ok)) {
164 qCWarning(C_VALIDATOR).noquote()
165 <<
debugString(c) <<
"Invalid maximum comparison value:" << d->max;
167 if ((val < min) || (val > max)) {
170 QVariantMap{{QStringLiteral(
"val"), val},
171 {QStringLiteral(
"min"), min},
172 {QStringLiteral(
"max"), max}});
173 qCDebug(C_VALIDATOR).noquote() <<
debugString(c) <<
"String length" << val
174 <<
"is not between" << min <<
"and" << max;
182 qCWarning(C_VALIDATOR).noquote()
183 <<
debugString(c) <<
"The comparison type" << d->type <<
"is not supported";
189 if (d->type != QMetaType::QString) {
190 const QVariant _v = d->valueToNumber(c, v, d->type);
197 result.
value.setValue(v);
208 const QVariant &errorData)
const
214 const QVariantMap map = errorData.toMap();
217 case QMetaType::Char:
218 case QMetaType::Short:
220 case QMetaType::Long:
221 case QMetaType::LongLong:
222 case QMetaType::QString:
223 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toLongLong());
224 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toLongLong());
226 case QMetaType::UChar:
227 case QMetaType::UShort:
228 case QMetaType::UInt:
229 case QMetaType::ULong:
230 case QMetaType::ULongLong:
231 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toULongLong());
232 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toULongLong());
234 case QMetaType::Float:
235 case QMetaType::Double:
236 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toDouble());
237 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toDouble());
244 const QString _label =
label(c);
246 if (_label.isEmpty()) {
247 if (d->type == QMetaType::QString) {
248 error = c->
translate(
"Cutelyst::ValidatorBetween",
249 "The text must be between %1 and %2 characters long.")
253 c->
translate(
"Cutelyst::ValidatorBetween",
"The value must be between %1 and %2.")
257 if (d->type == QMetaType::QString) {
259 "Cutelyst::ValidatorBetween",
260 "The text in the “%1“ field must be between %2 and %3 characters long.")
261 .arg(_label, min, max);
263 error = c->
translate(
"Cutelyst::ValidatorBetween",
264 "The value in the “%1” field must be between %2 and %3.")
265 .arg(_label, min, max);
276 int field = errorData.toInt();
277 const QString _label =
label(c);
280 if (_label.isEmpty()) {
281 error = c->
translate(
"Cutelyst::ValidatorBetween",
282 "The minimum comparison value is not valid.");
285 error = c->
translate(
"Cutelyst::ValidatorBetween",
286 "The minimum comparison value for the “%1” field is not valid.")
289 }
else if (
field == 0) {
291 if (_label.isEmpty()) {
292 error = c->
translate(
"Cutelyst::ValidatorBetween",
293 "The comparison type with ID %1 is not supported.")
294 .arg(
static_cast<int>(d->type));
298 c->
translate(
"Cutelyst::ValidatorBetween",
299 "The comparison type with ID %1 for the “%2” field is not supported.")
300 .arg(QString::number(
static_cast<int>(d->type)), _label);
302 }
else if (
field == 1) {
303 if (_label.isEmpty()) {
304 error = c->
translate(
"Cutelyst::ValidatorBetween",
305 "The maximum comparison value is not valid.");
308 error = c->
translate(
"Cutelyst::ValidatorBetween",
309 "The maximum comparison value for the “%1” field is not valid.")
323 const QString _label =
label(c);
324 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
325 if (_label.isEmpty()) {
326 error = c->
translate(
"Cutelyst::ValidatorBetween",
327 "Failed to parse the input value into a floating point number.");
330 error = c->
translate(
"Cutelyst::ValidatorBetween",
331 "Failed to parse the input value for the “%1” field into a "
332 "floating point number.")
336 if (_label.isEmpty()) {
337 error = c->
translate(
"Cutelyst::ValidatorBetween",
338 "Failed to parse the input value into an integer number.");
343 "Cutelyst::ValidatorBetween",
344 "Failed to parse the input value for the “%1” field into an integer number.")
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 field() const noexcept
Returns the name of the field to validate.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
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
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.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.