19 #include "validatorsize_p.h" 24 ValidatorRule(*new ValidatorSizePrivate(field, type, size, 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,
"ValidatorSize: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
55 const qlonglong size = d->extractLongLong(c, params, d->
size, &ok);
56 if (Q_UNLIKELY(!ok)) {
58 qCWarning(C_VALIDATOR,
"ValidatorSize: Invalid comparison size for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
62 {QStringLiteral(
"val"), val},
63 {QStringLiteral(
"size"), size}
65 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);
73 case QMetaType::UShort:
75 case QMetaType::ULong:
76 case QMetaType::ULongLong:
78 const qulonglong val = v.toULongLong(&ok);
79 if (Q_UNLIKELY(!ok)) {
81 qCWarning(C_VALIDATOR,
"ValidatorSize: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
83 const qulonglong size = d->extractULongLong(c, params, d->
size, &ok);
84 if (Q_UNLIKELY(!ok)) {
86 qCWarning(C_VALIDATOR,
"ValidatorSize: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
90 {QStringLiteral(
"val"), val},
91 {QStringLiteral(
"size"), size}
93 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);
101 case QMetaType::Float:
102 case QMetaType::Double:
104 const double val = v.toDouble(&ok);
105 if (Q_UNLIKELY(!ok)) {
107 qCWarning(C_VALIDATOR,
"ValidatorSize: Failed to parse value of field %s into number at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
109 const double size = d->extractDouble(c, params, d->
size, &ok);
110 if (Q_UNLIKELY(!ok)) {
112 qCWarning(C_VALIDATOR,
"ValidatorSize: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
116 {QStringLiteral(
"val"), val},
117 {QStringLiteral(
"size"), size}
119 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);
127 case QMetaType::QString:
129 const qlonglong val = static_cast<qlonglong>(v.length());
130 const qlonglong size = d->extractLongLong(c, params, d->
size, &ok);
131 if (Q_UNLIKELY(!ok)) {
133 qCWarning(C_VALIDATOR,
"ValidatorSize: Invalid maximum comparison value for field %s in %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
137 {QStringLiteral(
"val"), val},
138 {QStringLiteral(
"size"), size}
140 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);
148 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()));
154 if (d->type != QMetaType::QString) {
155 const QVariant _v = d->valueToNumber(c, v, d->type);
162 result.
value.setValue<QString>(v);
178 const QVariantMap map = errorData.toMap();
181 case QMetaType::Short:
183 case QMetaType::Long:
184 case QMetaType::LongLong:
185 case QMetaType::QString:
186 size = c->
locale().toString(map.value(QStringLiteral(
"size")).toLongLong());
188 case QMetaType::UShort:
189 case QMetaType::UInt:
190 case QMetaType::ULong:
191 case QMetaType::ULongLong:
192 size = c->
locale().toString(map.value(QStringLiteral(
"size")).toULongLong());
194 case QMetaType::Float:
195 case QMetaType::Double:
196 size = c->
locale().toString(map.value(QStringLiteral(
"size")).toDouble());
203 const QString _label =
label(c);
205 if (_label.isEmpty()) {
206 if (d->type == QMetaType::QString) {
208 error = c->
translate(
"Cutelyst::ValidatorSize",
"The text must be exactly %1 characters long.").arg(size);
211 error = c->
translate(
"Cutelyst::ValidatorSize",
"The value must be %1.").arg(size);
214 if (d->type == QMetaType::QString) {
216 error = c->
translate(
"Cutelyst::ValidatorSize",
"The text in the “%1“ field must be exactly %2 characters long.").arg(_label, size);
219 error = c->
translate(
"Cutelyst::ValidatorSize",
"The value in the “%1” field must be %2.").arg(_label, size);
230 int field = errorData.toInt();
231 const QString _label =
label(c);
235 if (_label.isEmpty()) {
236 error = c->
translate(
"Cutelyst::ValidatorSize",
"The comparison type with ID %1 is not supported.").arg(static_cast<int>(d->type));
238 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);
240 }
else if (
field == 1) {
241 if (_label.isEmpty()) {
242 error = c->
translate(
"Cutelyst::ValidatorSize",
"The comparison value is not valid.");
245 error = c->
translate(
"Cutelyst::ValidatorSize",
"The comparison value for the “%1” field is not valid.").arg(_label);
258 const QString _label =
label(c);
259 if ((d->type == QMetaType::Float) || (d->type == QMetaType::Double)) {
260 if (_label.isEmpty()) {
261 error = c->
translate(
"Cutelyst::ValidatorSize",
"Failed to parse the input value into a floating point number.");
264 error = c->
translate(
"Cutelyst::ValidatorSize",
"Failed to parse the input value for the “%1” field into a floating point number.").arg(_label);
267 if (_label.isEmpty()) {
268 error = c->
translate(
"Cutelyst::ValidatorSize",
"Failed to parse the input value into an integer number.");
271 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.
Stores custom error messages and the input field label.
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.
~ValidatorSize() override
Deconstructs the size validator.
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.
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.
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.
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.