cutelyst 5.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatordigits.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2025 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatordigits_p.h"
7
8using namespace Cutelyst;
9
11 const QVariant &length,
12 const Cutelyst::ValidatorMessages &messages,
13 const QString &defValKey)
14 : ValidatorRule(*new ValidatorDigitsPrivate(field, length, messages, defValKey))
15{
16}
17
19
21{
23
24 Q_D(const ValidatorDigits);
25
26 const QString v = value(params);
27 bool ok = false;
28 const qlonglong _length = ValidatorDigitsPrivate::extractLongLong(c, params, d->length, &ok);
29 if (!ok) {
30 qCDebug(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison length";
32 return result;
33 }
34
35 if (!v.isEmpty()) {
36
37 if (Q_LIKELY(ValidatorDigits::validate(v, _length))) {
38 if ((_length > 0) && (v.length() != _length)) {
39 result.errorMessage = validationError(c, _length);
40 qCDebug(C_VALIDATOR).noquote()
41 << debugString(c) << "Does not contain exactly" << _length
42 << "digits:" << v.length() << "!=" << _length;
43 } else {
44 result.value.setValue(v);
45 }
46 } else {
47 result.errorMessage = validationError(c, _length);
48 qCDebug(C_VALIDATOR).noquote().nospace()
49 << debugString(c) << " Does not only contain digits: \"" << v << "\"";
50 }
51
52 } else {
53 defaultValue(c, &result);
54 }
55
56 return result;
57}
58
60{
61 cb(validate(c, params));
62}
63
64bool ValidatorDigits::validate(const QString &value, qsizetype length)
65{
66 bool allDigits = std::ranges::all_of(value, [](const QChar &ch) { return ch.isDigit(); });
67
68 return allDigits && ((length <= 0) || (length == value.length()));
69}
70
72{
73 const QString _label = label(c);
74 const int _length = errorData.toInt();
75
76 if (_label.isEmpty()) {
77 if (_length > 0) {
78 //% "Must contain exactly %n digit(s)."
79 return c->qtTrId("cutelyst-valdigits-genvalerr-length", _length);
80 } else {
81 //% "Must only contain digits."
82 return c->qtTrId("cutelyst-valdigits-genvalerr");
83 }
84 } else {
85 if (_length > 0) {
86 //: %1 will be replaced by the field label
87 //% "The “%1” field must contain exactly %n digit(s)."
88 return c->qtTrId("cutelyst-valdigits-genvalerr-length-label", _length).arg(_label);
89 } else {
90 //: %1 will be replaced by the field label
91 //% "The “%1” field must only contain digits."
92 return c->qtTrId("cutelyst-valdigits-genvalerr-label").arg(_label);
93 }
94 }
95}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:657
Checks for digits only with optional length check.
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorDigits(const QString &field, const QVariant &length=-1, const ValidatorMessages &messages={}, const QString &defValKey={})
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(const Context *c) const
QString debugString(const Context *c) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
static bool validate(const QString &value, qsizetype length=-1)
Returns true if value only contains digits.
The Cutelyst namespace holds all public Cutelyst API.
bool isDigit(char32_t ucs4)
QString arg(Args &&... args) const const
bool isEmpty() const const
qsizetype length() const const
void setValue(QVariant &&value)
int toInt(bool *ok) const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.