19 #include "validatormax_p.h" 24 ValidatorRule(*new ValidatorMaxPrivate(field, type, max, messages, defValKey))
36 const QString v =
value(params);
45 case QMetaType::Short:
48 case QMetaType::LongLong:
50 const qlonglong val = c->
locale().toLongLong(v, &ok);
51 if (Q_UNLIKELY(!ok)) {
53 qCWarning(C_VALIDATOR,
"ValidatorMax: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
55 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
56 if (Q_UNLIKELY(!ok)) {
58 qCWarning(C_VALIDATOR,
"ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
62 {QStringLiteral(
"val"), val},
63 {QStringLiteral(
"max"), max}
65 qCDebug(C_VALIDATOR,
"ValidatorMax: Validation failed for field %s in %s::%s: %lli is not smaller than %lli.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, max);
73 case QMetaType::UChar:
74 case QMetaType::UShort:
76 case QMetaType::ULong:
77 case QMetaType::ULongLong:
79 const qulonglong val = v.toULongLong(&ok);
80 if (Q_UNLIKELY(!ok)) {
82 qCWarning(C_VALIDATOR,
"ValidatorMax: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
84 const qulonglong max = d->extractULongLong(c, params, d->max, &ok);
85 if (Q_UNLIKELY(!ok)) {
87 qCWarning(C_VALIDATOR,
"ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
91 {QStringLiteral(
"val"), val},
92 {QStringLiteral(
"max"), max}
94 qCDebug(C_VALIDATOR,
"ValidatorMax: Validation failed for field %s in %s::%s: %llu is not smaller than %llu.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, max);
102 case QMetaType::Float:
103 case QMetaType::Double:
105 const double val = v.toDouble(&ok);
106 if (Q_UNLIKELY(!ok)) {
108 qCWarning(C_VALIDATOR,
"ValidatorMax: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
110 const double max = d->extractDouble(c, params, d->max, &ok);
111 if (Q_UNLIKELY(!ok)) {
113 qCWarning(C_VALIDATOR,
"ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
117 {QStringLiteral(
"val"), val},
118 {QStringLiteral(
"max"), max}
120 qCDebug(C_VALIDATOR,
"ValidatorMax: Validation failed for field %s in %s::%s: %f is not smaller than %f.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, max);
128 case QMetaType::QString:
130 const qlonglong val =
static_cast<qlonglong
>(v.length());
131 const qlonglong max = d->extractLongLong(c, params, d->max, &ok);
132 if (Q_UNLIKELY(!ok)) {
134 qCWarning(C_VALIDATOR,
"ValidatorMax: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
138 {QStringLiteral(
"val"), val},
139 {QStringLiteral(
"max"), max}
141 qCDebug(C_VALIDATOR,
"ValidatorMax: Validation failed for field %s in %s::%s: string length %lli is not smaller than %lli.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), val, max);
149 qCWarning(C_VALIDATOR,
"ValidatorMax: 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()));
155 if (d->type != QMetaType::QString) {
156 const QVariant _v = d->valueToNumber(c, v, d->type);
163 result.
value.setValue<QString>(v);
179 const QVariantMap map = errorData.toMap();
182 case QMetaType::Char:
183 case QMetaType::Short:
185 case QMetaType::Long:
186 case QMetaType::LongLong:
187 case QMetaType::QString:
188 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toLongLong());
190 case QMetaType::UChar:
191 case QMetaType::UShort:
192 case QMetaType::UInt:
193 case QMetaType::ULong:
194 case QMetaType::ULongLong:
195 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toULongLong());
197 case QMetaType::Float:
198 case QMetaType::Double:
199 max = c->
locale().toString(map.value(QStringLiteral(
"max")).toDouble());
206 const QString _label =
label(c);
208 if (_label.isEmpty()) {
209 if (d->type == QMetaType::QString) {
210 error = c->
translate(
"Cutelyst::ValidatorMax",
"The text must be shorter than %1 characters.").arg(max);
212 error = c->
translate(
"Cutelyst::ValidatorMax",
"The value must be lower than %1.").arg(max);
215 if (d->type == QMetaType::QString) {
216 error = c->
translate(
"Cutelyst::ValidatorMax",
"The text in the “%1“ field must be shorter than %2 characters.").arg(_label, max);
218 error = c->
translate(
"Cutelyst::ValidatorMax",
"The value in the “%1” field must be lower than %2.").arg(_label, max);
229 int field = errorData.toInt();
230 const QString _label =
label(c);
234 if (_label.isEmpty()) {
235 error = c->
translate(
"Cutelyst::ValidatorMax",
"The comparison type with ID %1 is not supported.").arg(static_cast<int>(d->type));
237 error = c->
translate(
"Cutelyst::ValidatorMax",
"The comparison type with ID %1 for the “%2” field is not supported.").arg(QString::number(static_cast<int>(d->type)), _label);
239 }
else if (field == 1) {
240 if (_label.isEmpty()) {
241 error = c->
translate(
"Cutelyst::ValidatorMax",
"The maximum comparison value is not valid.");
243 error = c->
translate(
"Cutelyst::ValidatorMax",
"The maximum comparison value for the “%1” field is not valid.").arg(_label);
256 const QString _label =
label(c);
257 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
258 if (_label.isEmpty()) {
259 error = c->
translate(
"Cutelyst::ValidatorMax",
"Failed to parse the input value into a floating point number.");
261 error = c->
translate(
"Cutelyst::ValidatorMax",
"Failed to parse the input value for the “%1” field into a floating point number.").arg(_label);
264 if (_label.isEmpty()) {
265 error = c->
translate(
"Cutelyst::ValidatorMax",
"Failed to parse the input value into an integer number.");
267 error = c->
translate(
"Cutelyst::ValidatorMax",
"Failed to parse the input value for the “%1” field into an integer number.").arg(_label);
QMap< QString, QString > ParamsMultiMap
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occured while parsing input.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Stores custom error messages and the input field label.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error message for validation data errors.
QString genericParsingError(Context *c, const QVariant &errorData) const override
Returns a generic error message for input value parsing errors.
ValidatorMax(const QString &field, QMetaType::Type type, const QVariant &max, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new max validator.
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.
The Cutelyst namespace holds all public Cutelyst API.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
QString field() const
Returns the name of the field to validate.
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
Checks if a value is not bigger or longer than a maximum value.
Contains the result of a single input parameter validation.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message.
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 ...
~ValidatorMax()
Deconstructs the max validator.