Cutelyst  1.11.0
validatordigitsbetween.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 "validatordigitsbetween_p.h"
20 
21 #include <QRegularExpression>
22 
23 using namespace Cutelyst;
24 
25 ValidatorDigitsBetween::ValidatorDigitsBetween(const QString &field, int min, int max, const QString &label, const QString &customError) :
26  ValidatorRule(*new ValidatorDigitsBetweenPrivate(field, min, max, label, customError))
27 {
28 }
29 
30 ValidatorDigitsBetween::ValidatorDigitsBetween(ValidatorDigitsBetweenPrivate &dd) :
31  ValidatorRule(dd)
32 {
33 }
34 
36 {
37 }
38 
40 {
41  QString result;
42 
43  Q_D(const ValidatorDigitsBetween);
44 
45  if (!value().isEmpty()) {
46  if (value().contains(QRegularExpression(QStringLiteral("^[0-9]+$")))) {
47 
48  if ((d->min > 0) && (d->max > d->min)) {
49  if ((value().length() < d->min) || (value().length() > d->max)) {
50  result = validationError();
51  }
52  }
53 
54  } else {
55  result = validationError();
56  }
57  }
58 
59  return result;
60 }
61 
63 {
64  QString error;
65 
66  Q_D(const ValidatorDigitsBetween);
67 
68  if (label().isEmpty()) {
69  if (d->min < 1 || d->max < 1) {
70  error = QStringLiteral("Must only contain digits.");
71  } else {
72  error = QStringLiteral("Must only contain digits with a length between %1 and %2.").arg(QString::number(d->min), QString::number(d->max));
73  }
74  } else {
75  if (d->min < 1 || d->max < 1) {
76  error = QStringLiteral("The “%1” field must only contain digits.").arg(label());
77  } else {
78  error = QStringLiteral("The “%1” field must only contain digits with a length between %2 and %3.").arg(label(), QString::number(d->min), QString::number(d->max));
79  }
80  }
81 
82  return error;
83 }
84 
86 {
88  d->min = min;
89 }
90 
92 {
94  d->max = max;
95 }
~ValidatorDigitsBetween()
Deconstructs the digits between validator.
void setMax(int max)
Sets the maximum length.
QString value() const
Returns the field value.
Checks for digits only with a length between min and max.
void setMin(int min)
Sets the minimum length.
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.
QString genericValidationError() const override
Returns a generic error message.
QString label() const
Returns the human readable field label used for generic error messages.
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...
ValidatorDigitsBetween(const QString &field, int min, int max, const QString &label=QString(), const QString &customError=QString())
Constructs a new digits between validator.