cutelyst  4.4.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 
8 using 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 {
22  ValidatorReturnType result;
23 
24  Q_D(const ValidatorDigits);
25 
26  const QString v = value(params);
27  bool ok = false;
28  const qlonglong _length = d->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 
59 bool ValidatorDigits::validate(const QString &value, qsizetype length)
60 {
61  for (const QChar &ch : value) {
62  const ushort &uc = ch.unicode();
63  if (!((uc >= ValidatorRulePrivate::ascii_0) && (uc <= ValidatorRulePrivate::ascii_9))) {
64  return false;
65  }
66  }
67 
68  if ((length > 0) && (length != value.length())) {
69  return false;
70  }
71 
72  return true;
73 }
74 
76 {
77  const QString _label = label(c);
78  const int _length = errorData.toInt();
79 
80  if (_label.isEmpty()) {
81  if (_length > 0) {
82  //% "Must contain exactly %n digit(s)."
83  return c->qtTrId("cutelyst-valdigits-genvalerr-length", _length);
84  } else {
85  //% "Must only contain digits."
86  return c->qtTrId("cutelyst-valdigits-genvalerr");
87  }
88  } else {
89  if (_length > 0) {
90  //: %1 will be replaced by the field label
91  //% "The “%1” field must contain exactly %n digit(s)."
92  return c->qtTrId("cutelyst-valdigits-genvalerr-length-label", _length).arg(_label);
93  } else {
94  //: %1 will be replaced by the field label
95  //% "The “%1” field must only contain digits."
96  return c->qtTrId("cutelyst-valdigits-genvalerr-label").arg(_label);
97  }
98  }
99 }
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 optional length check.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorDigits(const QString &field, const QVariant &length=-1, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
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, qsizetype length=-1)
Returns true if value only contains digits.
The Cutelyst namespace holds all public Cutelyst API.
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.
Definition: validatorrule.h:49