6 #include "validatorsize_p.h" 11 ValidatorRule(*new ValidatorSizePrivate(field, type, size, messages, defValKey))
32 case QMetaType::Short:
35 case QMetaType::LongLong:
38 if (Q_UNLIKELY(!ok)) {
40 qCWarning(C_VALIDATOR,
"ValidatorSize: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
42 const qlonglong size = d->extractLongLong(c, params, d->
size, &ok);
43 if (Q_UNLIKELY(!ok)) {
45 qCWarning(C_VALIDATOR,
"ValidatorSize: Invalid comparison size for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
49 {QStringLiteral(
"val"), val},
50 {QStringLiteral(
"size"), size}
52 qCDebug(C_VALIDATOR,
"ValidatorSize: Validation failed for field %s in %s::%s: value is not %lli.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), size);
60 case QMetaType::UShort:
62 case QMetaType::ULong:
63 case QMetaType::ULongLong:
66 if (Q_UNLIKELY(!ok)) {
68 qCWarning(C_VALIDATOR,
"ValidatorSize: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
70 const qulonglong size = d->extractULongLong(c, params, d->
size, &ok);
71 if (Q_UNLIKELY(!ok)) {
73 qCWarning(C_VALIDATOR,
"ValidatorSize: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
77 {QStringLiteral(
"val"), val},
78 {QStringLiteral(
"size"), size}
80 qCDebug(C_VALIDATOR,
"ValidatorSize: Validation failed for field %s in %s::%s: value is not %llu.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), size);
88 case QMetaType::Float:
89 case QMetaType::Double:
92 if (Q_UNLIKELY(!ok)) {
94 qCWarning(C_VALIDATOR,
"ValidatorSize: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
96 const double size = d->extractDouble(c, params, d->
size, &ok);
97 if (Q_UNLIKELY(!ok)) {
99 qCWarning(C_VALIDATOR,
"ValidatorSize: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
103 {QStringLiteral(
"val"), val},
104 {QStringLiteral(
"size"), size}
106 qCDebug(C_VALIDATOR,
"ValidatorSize: Validation failed for field %s in %s::%s: value is not %f.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), size);
114 case QMetaType::QString:
116 const qlonglong val =
static_cast<qlonglong
>(v.
length());
117 const qlonglong size = d->extractLongLong(c, params, d->
size, &ok);
118 if (Q_UNLIKELY(!ok)) {
120 qCWarning(C_VALIDATOR,
"ValidatorSize: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
124 {QStringLiteral(
"val"), val},
125 {QStringLiteral(
"size"), size}
127 qCDebug(C_VALIDATOR,
"ValidatorSize: Validation failed for field %s in %s::%s: string length is not %lli.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), size);
135 qCWarning(C_VALIDATOR,
"ValidatorSize: 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()));
141 if (d->type != QMetaType::QString) {
142 const QVariant _v = d->valueToNumber(c, v, d->type);
165 const QVariantMap map = errorData.
toMap();
168 case QMetaType::Short:
170 case QMetaType::Long:
171 case QMetaType::LongLong:
172 case QMetaType::QString:
173 size = c->
locale().
toString(map.value(QStringLiteral(
"size")).toLongLong());
175 case QMetaType::UShort:
176 case QMetaType::UInt:
177 case QMetaType::ULong:
178 case QMetaType::ULongLong:
179 size = c->
locale().
toString(map.value(QStringLiteral(
"size")).toULongLong());
181 case QMetaType::Float:
182 case QMetaType::Double:
183 size = c->
locale().
toString(map.value(QStringLiteral(
"size")).toDouble());
193 if (d->type == QMetaType::QString) {
195 error = c->
translate(
"Cutelyst::ValidatorSize",
"The text must be exactly %1 characters long.").
arg(size);
198 error = c->
translate(
"Cutelyst::ValidatorSize",
"The value must be %1.").
arg(size);
201 if (d->type == QMetaType::QString) {
203 error = c->
translate(
"Cutelyst::ValidatorSize",
"The text in the “%1“ field must be exactly %2 characters long.").
arg(_label, size);
206 error = c->
translate(
"Cutelyst::ValidatorSize",
"The value in the “%1” field must be %2.").
arg(_label, size);
223 error = c->
translate(
"Cutelyst::ValidatorSize",
"The comparison type with ID %1 is not supported.").
arg(static_cast<int>(d->type));
225 error = c->
translate(
"Cutelyst::ValidatorSize",
"The comparison type with ID %1 for the “%2” field is not supported.").
arg(
QString::number(static_cast<int>(d->type)), _label);
227 }
else if (
field == 1) {
229 error = c->
translate(
"Cutelyst::ValidatorSize",
"The comparison value is not valid.");
232 error = c->
translate(
"Cutelyst::ValidatorSize",
"The comparison value for the “%1” field is not valid.").
arg(_label);
246 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
248 error = c->
translate(
"Cutelyst::ValidatorSize",
"Failed to parse the input value into a floating point number.");
251 error = c->
translate(
"Cutelyst::ValidatorSize",
"Failed to parse the input value for the “%1” field into a floating point number.").
arg(_label);
255 error = c->
translate(
"Cutelyst::ValidatorSize",
"Failed to parse the input value into an integer number.");
258 error = c->
translate(
"Cutelyst::ValidatorSize",
"Failed to parse the input value for the “%1” field into an integer number.").
arg(_label);
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
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.
QString toString(qlonglong i) const const
Stores custom error messages and the input field label.
double toDouble(bool *ok) const const
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
The field under validation must have a size matching the given value.
QString number(int n, int base)
int toInt(bool *ok) const const
~ValidatorSize() override
Deconstructs the size validator.
qlonglong toLongLong(const QString &s, bool *ok) const const
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error message for validation data errors.
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.
qulonglong toULongLong(bool *ok, int base) const const
QLocale locale() const noexcept
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
void setValue(const T &value)
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
QMap< QString, QVariant > toMap() const const
QString field() const
Returns the name of the field to validate.
bool isValid() const const
Contains the result of a single input parameter validation.
ValidatorSize(const QString &field, QMetaType::Type type, const QVariant &size, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new size validator.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
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 genericParsingError(Context *c, const QVariant &errorData) const override
Returns a generic error message for input value parsing errors.