6#include "validatorsize_p.h"
15 :
ValidatorRule(*new ValidatorSizePrivate(field, type, size, messages, defValKey))
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()),
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()),
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()),
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()));
201 const QVariant _v = d->valueToNumber(c, v, d->type);
224 const QVariantMap map = errorData.
toMap();
232 size = c->
locale().
toString(map.value(QStringLiteral(
"size")).toLongLong());
238 size = c->
locale().
toString(map.value(QStringLiteral(
"size")).toULongLong());
242 size = c->
locale().
toString(map.value(QStringLiteral(
"size")).toDouble());
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);
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.")
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.")
299 }
else if (
field == 1) {
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.")
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.")
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.
qlonglong toLongLong(const QString &s, bool *ok) const const
QString toString(qlonglong i) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
QString number(int n, int base)
double toDouble(bool *ok) const const
qulonglong toULongLong(bool *ok, int base) const const
bool isValid() const const
void setValue(const T &value)
int toInt(bool *ok) const const
QMap< QString, QVariant > toMap() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.