cutelyst  5.0.1
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-2025 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatordigitsbetween_p.h"
7 
8 using 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 {
23  ValidatorReturnType result;
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 = ValidatorDigitsBetweenPrivate::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 = ValidatorDigitsBetweenPrivate::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 
73  const ParamsMultiMap &params,
74  ValidatorRtFn cb) const
75 {
76  cb(validate(c, params));
77 }
78 
79 bool ValidatorDigitsBetween::validate(const QString &value, int min, int max)
80 {
81  bool allDigits = std::ranges::all_of(value, [](const QChar &ch) { return ch.isDigit(); });
82 
83  return allDigits && (value.length() >= min) && (value.length() <= max);
84 }
85 
87 {
88  const QVariantList list = errorData.toList();
89  const QString min = list.at(0).toString();
90  const QString max = list.at(1).toString();
91  const QString _label = label(c);
92 
93  if (_label.isEmpty()) {
94  //% "Must contain between %1 and %2 digits."
95  return c->qtTrId("cutelyst-valdigitsbetween-genvalerr").arg(min, max);
96  } else {
97  //: %1 will be replaced by the field label
98  //% "The “%1” field must contain between %2 and %3 digits."
99  return c->qtTrId("cutelyst-valdigitsbetween-genvalerr-label").arg(_label, min, max);
100  }
101 }
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
Stores custom error messages and the input field label.
QList< QVariant > toList() const 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 Context.
Definition: context.h:42
void defaultValue(Context *c, ValidatorReturnType *result) const
Checks for digits only with a length between min and max.
bool isEmpty() const const
The Cutelyst namespace holds all public Cutelyst API.
QString debugString(const Context *c) const
Base class for all validator rules.
QString value(const ParamsMultiMap &params) const
QString label(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
Definition: validatorrule.h:82
QString validationError(Context *c, const QVariant &errorData={}) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:658
bool isDigit(char32_t ucs4)
const QChar at(qsizetype position) const const
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
qsizetype length() const const
Contains the result of a single input parameter validation.
Definition: validatorrule.h:52
ValidatorDigitsBetween(const QString &field, const QVariant &min, const QVariant &max, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey={})
QString arg(Args &&... args) const const
void setValue(QVariant &&value)