6 #include "validatormin_p.h"
11 ValidatorRule(*new ValidatorMinPrivate(field, type, min, messages, defValKey))
23 const QString v =
value(params);
33 case QMetaType::Short:
36 case QMetaType::LongLong:
38 const qlonglong val = c->
locale().toLongLong(v, &ok);
39 if (Q_UNLIKELY(!ok)) {
41 qCWarning(C_VALIDATOR,
"ValidatorMin: 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,
"ValidatorMin: Invalid minimum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
50 {QStringLiteral(
"val"), val},
51 {QStringLiteral(
"min"), min}
53 qCDebug(C_VALIDATOR,
"ValidatorMin: Validation failed for field %s in %s::%s: %lli is not greater than %lli.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, min);
61 case QMetaType::UChar:
62 case QMetaType::UShort:
64 case QMetaType::ULong:
65 case QMetaType::ULongLong:
67 const qulonglong val = v.toULongLong(&ok);
68 if (Q_UNLIKELY(!ok)) {
70 qCWarning(C_VALIDATOR,
"ValidatorMin: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
72 const qulonglong min = d->extractULongLong(c, params, d->min, &ok);
73 if (Q_UNLIKELY(!ok)) {
75 qCWarning(C_VALIDATOR,
"ValidatorMin: Invalid minimum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
79 {QStringLiteral(
"val"), val},
80 {QStringLiteral(
"min"), min}
82 qCDebug(C_VALIDATOR,
"ValidatorMin: Validation failed for field %s in %s::%s: %llu is not greater than %llu.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, min);
90 case QMetaType::Float:
91 case QMetaType::Double:
93 const double val = v.toDouble(&ok);
94 if (Q_UNLIKELY(!ok)) {
96 qCWarning(C_VALIDATOR,
"ValidatorMin: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
98 const double min = d->extractDouble(c, params, d->min, &ok);
99 if (Q_UNLIKELY(!ok)) {
101 qCWarning(C_VALIDATOR,
"ValidatorMin: Invalid minimum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
105 {QStringLiteral(
"val"), val},
106 {QStringLiteral(
"min"), min}
108 qCDebug(C_VALIDATOR,
"ValidatorMin: Validation failed for field %s in %s::%s: %f is not greater than %f.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, min);
116 case QMetaType::QString:
118 const qlonglong val =
static_cast<qlonglong
>(v.length());
119 const qlonglong min = d->extractLongLong(c, params, d->min, &ok);
120 if (Q_UNLIKELY(!ok)) {
122 qCWarning(C_VALIDATOR,
"ValidatorMin: Invalid minimum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
126 {QStringLiteral(
"val"), val},
127 {QStringLiteral(
"min"), min}
129 qCDebug(C_VALIDATOR,
"ValidatorMin: Validation failed for field %s in %s::%s: string length %lli is not longer than %lli.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, min);
137 qCWarning(C_VALIDATOR,
"ValidatorMin: 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()));
143 if (d->type != QMetaType::QString) {
144 const QVariant _v = d->valueToNumber(c, v, d->type);
151 result.
value.setValue(v);
167 const QVariantMap map = errorData.toMap();
170 case QMetaType::Char:
171 case QMetaType::Short:
173 case QMetaType::Long:
174 case QMetaType::LongLong:
175 case QMetaType::QString:
176 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toLongLong());
178 case QMetaType::UChar:
179 case QMetaType::UShort:
180 case QMetaType::UInt:
181 case QMetaType::ULong:
182 case QMetaType::ULongLong:
183 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toULongLong());
185 case QMetaType::Float:
186 case QMetaType::Double:
187 min = c->
locale().toString(map.value(QStringLiteral(
"min")).toDouble());
194 const QString _label =
label(c);
196 if (_label.isEmpty()) {
197 if (d->type == QMetaType::QString) {
198 error = c->
translate(
"Cutelyst::ValidatorMin",
"The text must be longer than %1 characters.").arg(min);
200 error = c->
translate(
"Cutelyst::ValidatorMin",
"The value must be greater than %1.").arg(min);
203 if (d->type == QMetaType::QString) {
204 error = c->
translate(
"Cutelyst::ValidatorMin",
"The text in the “%1“ field must be longer than %2 characters.").arg(_label, min);
206 error = c->
translate(
"Cutelyst::ValidatorMin",
"The value in the “%1” field must be greater than %2.").arg(_label, min);
217 int field = errorData.toInt();
218 const QString _label =
label(c);
221 if (_label.isEmpty()) {
222 error = c->
translate(
"Cutelyst::ValidatorMin",
"The minimum comparison value is not valid.");
224 error = c->
translate(
"Cutelyst::ValidatorMin",
"The minimum comparison value for the “%1” field is not valid.").arg(_label);
226 }
else if (
field == 0) {
228 if (_label.isEmpty()) {
229 error = c->
translate(
"Cutelyst::ValidatorMin",
"The comparison type with ID %1 is not supported.").arg(
static_cast<int>(d->type));
231 error = c->
translate(
"Cutelyst::ValidatorMin",
"The comparison type with ID %1 for the “%2” field is not supported.").arg(QString::number(
static_cast<int>(d->type)), _label);
244 const QString _label =
label(c);
245 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
246 if (_label.isEmpty()) {
247 error = c->
translate(
"Cutelyst::ValidatorMin",
"Failed to parse the input value into a floating point number.");
249 error = c->
translate(
"Cutelyst::ValidatorMin",
"Failed to parse the input value for the “%1” field into a floating point number.").arg(_label);
252 if (_label.isEmpty()) {
253 error = c->
translate(
"Cutelyst::ValidatorMin",
"Failed to parse the input value into an integer number.");
255 error = c->
translate(
"Cutelyst::ValidatorMin",
"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 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 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 occured 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.
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.