6#include "validatormin_p.h"
14 const QString &defValKey)
15 :
ValidatorRule(*new ValidatorMinPrivate(field, type, min, messages, defValKey))
25 const QString v =
value(params);
35 case QMetaType::Short:
38 case QMetaType::LongLong:
40 const qlonglong val = c->
locale().toLongLong(v, &ok);
41 if (Q_UNLIKELY(!ok)) {
43 qCWarning(C_VALIDATOR).noquote().nospace()
44 <<
debugString(c) <<
" Failed to parse \"" << v <<
"\" into an integer number";
46 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
47 if (Q_UNLIKELY(!ok)) {
49 qCWarning(C_VALIDATOR).noquote()
50 <<
debugString(c) <<
"Invalid minimum comparison value";
55 QVariantMap{{QStringLiteral(
"val"), val},
56 {QStringLiteral(
"min"), min}});
57 qCDebug(C_VALIDATOR).noquote()
58 <<
debugString(c) << val <<
"is not greater than" << min;
65 case QMetaType::UChar:
66 case QMetaType::UShort:
68 case QMetaType::ULong:
69 case QMetaType::ULongLong:
71 const qulonglong val = v.toULongLong(&ok);
72 if (Q_UNLIKELY(!ok)) {
74 qCWarning(C_VALIDATOR).noquote().nospace()
76 <<
"\" into an unsigned integer number";
78 const qulonglong min = d->extractULongLong(c, params, d->min, &ok);
79 if (Q_UNLIKELY(!ok)) {
81 qCWarning(C_VALIDATOR).noquote()
82 <<
debugString(c) <<
"Invalid minimum comparison value";
87 QVariantMap{{QStringLiteral(
"val"), val},
88 {QStringLiteral(
"min"), min}});
89 qCDebug(C_VALIDATOR).noquote()
90 <<
debugString(c) << val <<
"is not greater than" << min;
97 case QMetaType::Float:
98 case QMetaType::Double:
100 const double val = v.toDouble(&ok);
101 if (Q_UNLIKELY(!ok)) {
103 qCWarning(C_VALIDATOR).noquote().nospace()
105 <<
"\" into a floating point number";
107 const double min = d->extractDouble(c, params, d->min, &ok);
108 if (Q_UNLIKELY(!ok)) {
110 qCWarning(C_VALIDATOR).noquote()
111 <<
debugString(c) <<
"Invalid minimum comparison value";
116 QVariantMap{{QStringLiteral(
"val"), val},
117 {QStringLiteral(
"min"), min}});
118 qCDebug(C_VALIDATOR).noquote()
119 <<
debugString(c) << val <<
"is not greater than" << min;
126 case QMetaType::QString:
128 const auto val =
static_cast<qlonglong
>(v.length());
129 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
130 if (Q_UNLIKELY(!ok)) {
132 qCWarning(C_VALIDATOR).noquote()
133 <<
debugString(c) <<
"Invalid minimum comparison value";
137 c, QVariantMap{{QStringLiteral(
"val"), val}, {QStringLiteral(
"min"), min}});
138 qCDebug(C_VALIDATOR).noquote()
139 <<
debugString(c) <<
"String length" << val <<
"is not longer than" << min;
146 qCWarning(C_VALIDATOR).noquote()
147 <<
debugString(c) <<
"The comparison type" << d->type <<
"is not supported";
153 if (d->type != QMetaType::QString) {
154 const QVariant _v = d->valueToNumber(c, v, d->type);
161 result.
value.setValue(v);
177 const QVariantMap map = errorData.toMap();
180 case QMetaType::Char:
181 case QMetaType::Short:
183 case QMetaType::Long:
184 case QMetaType::LongLong:
185 case QMetaType::QString:
186 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toLongLong());
188 case QMetaType::UChar:
189 case QMetaType::UShort:
190 case QMetaType::UInt:
191 case QMetaType::ULong:
192 case QMetaType::ULongLong:
193 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toULongLong());
195 case QMetaType::Float:
196 case QMetaType::Double:
197 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toDouble());
204 const QString _label =
label(c);
206 if (_label.isEmpty()) {
207 if (d->type == QMetaType::QString) {
208 error = c->
translate(
"Cutelyst::ValidatorMin",
209 "The text must be longer than %1 characters.")
212 error = c->
translate(
"Cutelyst::ValidatorMin",
"The value must be greater than %1.")
216 if (d->type == QMetaType::QString) {
217 error = c->
translate(
"Cutelyst::ValidatorMin",
218 "The text in the “%1“ field must be longer than %2 characters.")
221 error = c->
translate(
"Cutelyst::ValidatorMin",
222 "The value in the “%1” field must be greater than %2.")
234 int field = errorData.toInt();
235 const QString _label =
label(c);
238 if (_label.isEmpty()) {
239 error = c->
translate(
"Cutelyst::ValidatorMin",
240 "The minimum comparison value is not valid.");
242 error = c->
translate(
"Cutelyst::ValidatorMin",
243 "The minimum comparison value for the “%1” field is not valid.")
246 }
else if (
field == 0) {
248 if (_label.isEmpty()) {
249 error = c->
translate(
"Cutelyst::ValidatorMin",
250 "The comparison type with ID %1 is not supported.")
251 .arg(
static_cast<int>(d->type));
255 "The comparison type with ID %1 for the “%2” field is not supported.")
256 .arg(QString::number(
static_cast<int>(d->type)), _label);
269 const QString _label =
label(c);
270 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
271 if (_label.isEmpty()) {
272 error = c->
translate(
"Cutelyst::ValidatorMin",
273 "Failed to parse the input value into a floating point number.");
275 error = c->
translate(
"Cutelyst::ValidatorMin",
276 "Failed to parse the input value for the “%1” field into a "
277 "floating point number.")
281 if (_label.isEmpty()) {
282 error = c->
translate(
"Cutelyst::ValidatorMin",
283 "Failed to parse the input value into an integer number.");
287 "Cutelyst::ValidatorMin",
288 "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 is not smaller or shorter than a maximum value.
~ValidatorMin() override
Deconstructs the min validator.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error message for validation data errors.
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.
QString genericParsingError(Context *c, const QVariant &errorData) const override
Returns a generic error message for input value parsing errors.
ValidatorMin(const QString &field, QMetaType::Type type, const QVariant &min, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new min validator.
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.