Cutelyst  1.11.0
validatordigits.cpp
1 /*
2  * Copyright (C) 2017 Matthias Fehring <kontakt@buschmann23.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "validatordigits_p.h"
20 
21 #include <QRegularExpression>
22 
23 using namespace Cutelyst;
24 
25 ValidatorDigits::ValidatorDigits(const QString &field, int length, const QString &label, const QString &customError) :
26  ValidatorRule(*new ValidatorDigitsPrivate(field, length, label, customError))
27 {
28 }
29 
30 ValidatorDigits::ValidatorDigits(ValidatorDigitsPrivate &dd) :
31  ValidatorRule(dd)
32 {
33 }
34 
36 {
37 }
38 
40 {
41  QString result;
42 
43  Q_D(const ValidatorDigits);
44 
45  if (!value().isEmpty()) {
46 
47  if (value().contains(QRegularExpression(QStringLiteral("^[0-9]+$")))) {
48  if ((d->length > 0) && (value().length() != d->length)) {
49  result = validationError();
50  }
51  } else {
52  result = validationError();
53  }
54  }
55 
56  return result;
57 }
58 
60 {
61  QString error;
62 
63  Q_D(const ValidatorDigits);
64 
65  if (label().isEmpty()) {
66 
67  if (d->length > 0) {
68  error = QStringLiteral("Must only contain exactly %1 digits.").arg(d->length);
69  } else {
70  error = QStringLiteral("Must only contain digits.");
71  }
72 
73  } else {
74 
75  if (d->length > 0) {
76  error = QStringLiteral("The “%1” field must only contain exactly %2 digits.").arg(label(), QString::number(d->length));
77  } else {
78  error = QStringLiteral("The “%1” field must only contain digits.").arg(label());
79  }
80  }
81 
82  return error;
83 }
84 
86 {
87  Q_D(ValidatorDigits);
88  d->length = length;
89 }
Checks for digits only with optional length check.
QString value() const
Returns the field value.
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...
QString genericValidationError() const override
Returns a generic error message.
QString validationError() const
Returns a descriptive error message if validation failed.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validators.
void setLength(int length)
Sets the allowed length of the input data.
QString label() const
Returns the human readable field label used for generic error messages.
~ValidatorDigits()
Deconstructs the digits validator.
ValidatorDigits(const QString &field, int length=-1, const QString &label=QString(), const QString &customError=QString())
Constructs a new digits validator.