cutelyst 4.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-2023 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 int _length = d->extractInt(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
59bool ValidatorDigits::validate(const QString &value, int length)
60{
61 bool valid = true;
62
63 for (const QChar &ch : value) {
64 const ushort &uc = ch.unicode();
65 if (!((uc >= ValidatorRulePrivate::ascii_0) && (uc <= ValidatorRulePrivate::ascii_9))) {
66 valid = false;
67 break;
68 }
69 }
70
71 if (valid && (length > 0) && (length != value.length())) {
72 valid = false;
73 }
74
75 return valid;
76}
77
78QString ValidatorDigits::genericValidationError(Context *c, const QVariant &errorData) const
79{
80 QString error;
81
82 const QString _label = label(c);
83 const int _length = errorData.toInt();
84
85 if (_label.isEmpty()) {
86 if (_length > 0) {
87 error = c->translate(
88 "Cutelyst::ValidatorDigits", "Must contain exactly %n digit(s).", "", _length);
89 } else {
90 error = c->translate("Cutelyst::ValidatorDigits", "Must only contain digits.");
91 }
92 } else {
93 if (_length > 0) {
94 //: %1 will be replaced by the field label
95 error = c->translate("Cutelyst::ValidatorDigits",
96 "The “%1” field must contain exactly %n digit(s).",
97 "",
98 _length)
99 .arg(_label);
100 } else {
101 //: %1 will be replaced by the field label
102 error = c->translate("Cutelyst::ValidatorDigits",
103 "The “%1” field must only contain digits.")
104 .arg(_label);
105 }
106 }
107
108 return error;
109}
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
Checks for digits only with optional length check.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
~ValidatorDigits() override
Deconstructs the digits validator.
ValidatorDigits(const QString &field, const QVariant &length=-1, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new digits validator.
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 length=-1)
Returns true if value only contains digits.
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