Cutelyst  3.5.0
validatordigits.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 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 
10 ValidatorDigits::ValidatorDigits(const QString &field, const QVariant &length, const Cutelyst::ValidatorMessages &messages, const QString &defValKey) :
11  ValidatorRule(*new ValidatorDigitsPrivate(field, length, messages, defValKey))
12 {
13 }
14 
16 {
17 }
18 
20 {
21  ValidatorReturnType result;
22 
23  Q_D(const ValidatorDigits);
24 
25  const QString v = value(params);
26  bool ok = false;
27  int _length = d->extractInt(c, params, d->length, &ok);
28  if (!ok) {
30  return result;
31  }
32 
33  if (!v.isEmpty()) {
34 
35  if (Q_LIKELY(ValidatorDigits::validate(v, _length))) {
36  if ((_length > 0) && (v.length() != _length)) {
37  result.errorMessage = validationError(c, _length);
38  qCDebug(C_VALIDATOR, "ValidatorDigits: Validation failed for value \"%s\" in field %s at %s::%s: does not contain exactly %i digit(s).", qPrintable(v), qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), _length);
39  } else {
40  result.value.setValue(v);
41  }
42  } else {
43  result.errorMessage = validationError(c, _length);
44  qCDebug(C_VALIDATOR, "ValidatorDigits: Validation failed for value \"%s\" in field %s at %s::%s: does not only contain digits.", qPrintable(v), qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
45  }
46 
47  } else {
48  defaultValue(c, &result, "ValidatorDigits");
49  }
50 
51  return result;
52 }
53 
54 bool ValidatorDigits::validate(const QString &value, int length)
55 {
56  bool valid = true;
57 
58  for (const QChar &ch : value) {
59  const ushort &uc = ch.unicode();
60  if (!((uc > 47) && (uc < 58))) {
61  valid = false;
62  break;
63  }
64  }
65 
66  if (valid && (length > 0) && (length != value.length())) {
67  valid = false;
68  }
69 
70  return valid;
71 }
72 
74 {
75  QString error;
76 
77  const QString _label = label(c);
78  const int _length = errorData.toInt();
79 
80  if (_label.isEmpty()) {
81  if (_length > 0) {
82  error = c->translate("Cutelyst::ValidatorDigits", "Must contain exactly %n digit(s).", "", _length);
83  } else {
84  error = c->translate("Cutelyst::ValidatorDigits", "Must only contain digits.");
85  }
86  } else {
87  if (_length > 0) {
88  //: %1 will be replaced by the field label
89  error = c->translate("Cutelyst::ValidatorDigits", "The “%1” field must contain exactly %n digit(s).", "", _length).arg(_label);
90  } else {
91  //: %1 will be replaced by the field label
92  error = c->translate("Cutelyst::ValidatorDigits", "The “%1” field must only contain digits.").arg(_label);
93  }
94  }
95 
96  return error;
97 }
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Checks for digits only with optional length check.
~ValidatorDigits() override
Deconstructs the digits validator.
Stores custom error messages and the input field label.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
The Cutelyst Context.
Definition: context.h:38
int toInt(bool *ok) const const
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:471
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
static bool validate(const QString &value, int length=-1)
Returns true if value only contains digits.
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
ValidatorDigits(const QString &field, const QVariant &length=-1, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new digits validator.
int length() const const
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...