cutelyst 4.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatordigitsbetween.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatordigitsbetween_p.h"
7
8using namespace Cutelyst;
9
11 const QVariant &min,
12 const QVariant &max,
13 const ValidatorMessages &messages,
14 const QString &defValKey)
15 : ValidatorRule(*new ValidatorDigitsBetweenPrivate(field, min, max, messages, defValKey))
16{
17}
18
20
22{
24
25 Q_D(const ValidatorDigitsBetween);
26
27 const QString v = value(params);
28
29 bool ok = false;
30 int _max = 0;
31 int _min = d->extractInt(c, params, d->min, &ok);
32 if (!ok) {
34 qCWarning(C_VALIDATOR).noquote()
35 << debugString(c) << "Invalid minimum length comparison data";
36 return result;
37 } else {
38 _max = d->extractInt(c, params, d->max, &ok);
39 if (!ok) {
41 qCWarning(C_VALIDATOR).noquote()
42 << debugString(c) << "Invalid maximum length comparison data";
43 return result;
44 }
45 }
46
47 if (_min > _max) {
49 qCWarning(C_VALIDATOR).noquote()
50 << debugString(c) << "Minimum comparison length" << _min << "is larger than"
51 << "maximum comparison length" << _max;
52 return result;
53 }
54
55 if (!v.isEmpty()) {
56
57 if (Q_LIKELY(ValidatorDigitsBetween::validate(v, _min, _max))) {
58 result.value.setValue(v);
59 } else {
60 result.errorMessage = validationError(c, QVariantList{_min, _max});
61 qCDebug(C_VALIDATOR).noquote()
62 << debugString(c) << "Length of" << v.length() << "is not between" << _min << "and"
63 << _max << "and/or input value contains non-digit characters";
64 }
65
66 } else {
67 defaultValue(c, &result);
68 }
69
70 return result;
71}
72
73bool ValidatorDigitsBetween::validate(const QString &value, int min, int max)
74{
75 bool valid = true;
76
77 for (const QChar &ch : value) {
78 const ushort &uc = ch.unicode();
79 if (!((uc >= ValidatorRulePrivate::ascii_0) && (uc <= ValidatorRulePrivate::ascii_9))) {
80 valid = false;
81 break;
82 }
83 }
84
85 if (valid && ((value.length() < min) || (value.length() > max))) {
86 valid = false;
87 }
88
89 return valid;
90}
91
92QString ValidatorDigitsBetween::genericValidationError(Context *c, const QVariant &errorData) const
93{
94 QString error;
95
96 const QVariantList list = errorData.toList();
97 const QString min = list.at(0).toString();
98 const QString max = list.at(1).toString();
99 const QString _label = label(c);
100
101 if (_label.isEmpty()) {
102 error = c->translate("Cutelyst::ValidatorDigitsBetween",
103 "Must contain between %1 and %2 digits.")
104 .arg(min, max);
105 } else {
106 //: %1 will be replaced by the field label
107 error = c->translate("Cutelyst::ValidatorDigitsBetween",
108 "The “%1” field must contain between %2 and %3 digits.")
109 .arg(_label, min, max);
110 }
111
112 return error;
113}
The Cutelyst Context.
Definition: context.h:38
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
<Cutelyst/Plugins/Utils/validatordigitsbetween.h>
~ValidatorDigitsBetween() override
Deconstructs the digits between validator.
ValidatorDigitsBetween(const QString &field, const QVariant &min, const QVariant &max, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new digits between validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
void defaultValue(Context *c, ValidatorReturnType *result) 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 &params) 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.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
static bool validate(const QString &value, int min, int max)
Returns true if value only contains digits and has a length between min and max.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49