6#include "validatorsize_p.h"
14 const QString &defValKey)
15 :
ValidatorRule(*new ValidatorSizePrivate(field, type, size, messages, defValKey))
27 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,
45 "ValidatorSize: Failed to parse value of field %s into number at %s::%s.",
47 qPrintable(c->controllerName()),
48 qPrintable(c->actionName()));
50 const qlonglong size = d->extractLongLong(c, params, d->size, &ok);
51 if (Q_UNLIKELY(!ok)) {
53 qCWarning(C_VALIDATOR,
54 "ValidatorSize: Invalid comparison size for field %s in %s::%s.",
56 qPrintable(c->controllerName()),
57 qPrintable(c->actionName()));
62 QVariantMap{{QStringLiteral(
"val"), val},
63 {QStringLiteral(
"size"), size}});
65 "ValidatorSize: Validation failed for field %s in %s::%s: value is "
68 qPrintable(c->controllerName()),
69 qPrintable(c->actionName()),
77 case QMetaType::UShort:
79 case QMetaType::ULong:
80 case QMetaType::ULongLong:
82 const qulonglong val = v.toULongLong(&ok);
83 if (Q_UNLIKELY(!ok)) {
85 qCWarning(C_VALIDATOR,
86 "ValidatorSize: Failed to parse value of field %s into number at %s::%s.",
88 qPrintable(c->controllerName()),
89 qPrintable(c->actionName()));
91 const qulonglong size = d->extractULongLong(c, params, d->size, &ok);
92 if (Q_UNLIKELY(!ok)) {
96 "ValidatorSize: Invalid maximum comparison value for field %s in %s::%s.",
98 qPrintable(c->controllerName()),
99 qPrintable(c->actionName()));
104 QVariantMap{{QStringLiteral(
"val"), val},
105 {QStringLiteral(
"size"), size}});
107 "ValidatorSize: Validation failed for field %s in %s::%s: value is "
110 qPrintable(c->controllerName()),
111 qPrintable(c->actionName()),
119 case QMetaType::Float:
120 case QMetaType::Double:
122 const double val = v.toDouble(&ok);
123 if (Q_UNLIKELY(!ok)) {
125 qCWarning(C_VALIDATOR,
126 "ValidatorSize: Failed to parse value of field %s into number at %s::%s.",
128 qPrintable(c->controllerName()),
129 qPrintable(c->actionName()));
131 const double size = d->extractDouble(c, params, d->size, &ok);
132 if (Q_UNLIKELY(!ok)) {
136 "ValidatorSize: Invalid maximum comparison value for field %s in %s::%s.",
138 qPrintable(c->controllerName()),
139 qPrintable(c->actionName()));
144 QVariantMap{{QStringLiteral(
"val"), val},
145 {QStringLiteral(
"size"), size}});
147 "ValidatorSize: Validation failed for field %s in %s::%s: value is "
150 qPrintable(c->controllerName()),
151 qPrintable(c->actionName()),
159 case QMetaType::QString:
161 const qlonglong val =
static_cast<qlonglong
>(v.length());
162 const qlonglong size = d->extractLongLong(c, params, d->size, &ok);
163 if (Q_UNLIKELY(!ok)) {
165 qCWarning(C_VALIDATOR,
166 "ValidatorSize: Invalid maximum comparison value for field %s in %s::%s.",
168 qPrintable(c->controllerName()),
169 qPrintable(c->actionName()));
174 QVariantMap{{QStringLiteral(
"val"), val}, {QStringLiteral(
"size"), size}});
176 "ValidatorSize: Validation failed for field %s in %s::%s: string "
177 "length is not %lli.",
179 qPrintable(c->controllerName()),
180 qPrintable(c->actionName()),
188 qCWarning(C_VALIDATOR,
189 "ValidatorSize: The comparison type with ID %i for field %s at %s::%s is not "
191 static_cast<int>(d->type),
193 qPrintable(c->controllerName()),
194 qPrintable(c->actionName()));
200 if (d->type != QMetaType::QString) {
201 const QVariant _v = d->valueToNumber(c, v, d->type);
208 result.
value.setValue(v);
224 const QVariantMap map = errorData.toMap();
227 case QMetaType::Short:
229 case QMetaType::Long:
230 case QMetaType::LongLong:
231 case QMetaType::QString:
232 size = c->
locale().toString(map.value(QStringLiteral(
"size")).toLongLong());
234 case QMetaType::UShort:
235 case QMetaType::UInt:
236 case QMetaType::ULong:
237 case QMetaType::ULongLong:
238 size = c->
locale().toString(map.value(QStringLiteral(
"size")).toULongLong());
240 case QMetaType::Float:
241 case QMetaType::Double:
242 size = c->
locale().toString(map.value(QStringLiteral(
"size")).toDouble());
249 const QString _label =
label(c);
251 if (_label.isEmpty()) {
252 if (d->type == QMetaType::QString) {
254 error = c->
translate(
"Cutelyst::ValidatorSize",
255 "The text must be exactly %1 characters long.")
259 error = c->
translate(
"Cutelyst::ValidatorSize",
"The value must be %1.").arg(size);
262 if (d->type == QMetaType::QString) {
265 error = c->
translate(
"Cutelyst::ValidatorSize",
266 "The text in the “%1“ field must be exactly %2 characters long.")
272 c->
translate(
"Cutelyst::ValidatorSize",
"The value in the “%1” field must be %2.")
284 int field = errorData.toInt();
285 const QString _label =
label(c);
289 if (_label.isEmpty()) {
290 error = c->
translate(
"Cutelyst::ValidatorSize",
291 "The comparison type with ID %1 is not supported.")
292 .arg(
static_cast<int>(d->type));
296 "The comparison type with ID %1 for the “%2” field is not supported.")
297 .arg(QString::number(
static_cast<int>(d->type)), _label);
299 }
else if (
field == 1) {
300 if (_label.isEmpty()) {
301 error = c->
translate(
"Cutelyst::ValidatorSize",
"The comparison value is not valid.");
304 error = c->
translate(
"Cutelyst::ValidatorSize",
305 "The comparison value for the “%1” field is not valid.")
319 const QString _label =
label(c);
320 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
321 if (_label.isEmpty()) {
322 error = c->
translate(
"Cutelyst::ValidatorSize",
323 "Failed to parse the input value into a floating point number.");
326 error = c->
translate(
"Cutelyst::ValidatorSize",
327 "Failed to parse the input value for the “%1” field into a "
328 "floating point number.")
332 if (_label.isEmpty()) {
333 error = c->
translate(
"Cutelyst::ValidatorSize",
334 "Failed to parse the input value into an integer number.");
339 "Cutelyst::ValidatorSize",
340 "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
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 occurred 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 field under validation must have a size matching the given value.
~ValidatorSize() override
Deconstructs the size validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
ValidatorSize(const QString &field, QMetaType::Type type, const QVariant &size, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new size 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.
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
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.