cutelyst 4.3.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 qsizetype _max = 0;
31 qsizetype _min = d->extractSizeType(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->extractSizeType(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() << debugString(c) << "Minimum comparison length" << _min
50 << "is larger than" << "maximum comparison length" << _max;
51 return result;
52 }
53
54 if (!v.isEmpty()) {
55
56 if (Q_LIKELY(ValidatorDigitsBetween::validate(v, _min, _max))) {
57 result.value.setValue(v);
58 } else {
59 result.errorMessage = validationError(c, QVariantList{_min, _max});
60 qCDebug(C_VALIDATOR).noquote()
61 << debugString(c) << "Length of" << v.length() << "is not between" << _min << "and"
62 << _max << "and/or input value contains non-digit characters";
63 }
64
65 } else {
66 defaultValue(c, &result);
67 }
68
69 return result;
70}
71
72bool ValidatorDigitsBetween::validate(const QString &value, int min, int max)
73{
74 for (const QChar &ch : value) {
75 const ushort &uc = ch.unicode();
76 if (!((uc >= ValidatorRulePrivate::ascii_0) && (uc <= ValidatorRulePrivate::ascii_9))) {
77 return false;
78 }
79 }
80
81 if ((value.length() < min) || (value.length() > max)) {
82 return false;
83 }
84
85 return true;
86}
87
89{
90 const QVariantList list = errorData.toList();
91 const QString min = list.at(0).toString();
92 const QString max = list.at(1).toString();
93 const QString _label = label(c);
94
95 if (_label.isEmpty()) {
96 //% "Must contain between %1 and %2 digits."
97 return c->qtTrId("cutelyst-valdigitsbetween-genvalerr").arg(min, max);
98 } else {
99 //: %1 will be replaced by the field label
100 //% "The “%1” field must contain between %2 and %3 digits."
101 return c->qtTrId("cutelyst-valdigitsbetween-genvalerr-label").arg(_label, min, max);
102 }
103}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:656
Checks for digits only with a length between min and max.
ValidatorDigitsBetween(const QString &field, const QVariant &min, const QVariant &max, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
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.
QString arg(Args &&... args) const const
const QChar at(qsizetype position) const const
bool isEmpty() const const
qsizetype length() const const
void setValue(QVariant &&value)
QList< QVariant > toList() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.