Cutelyst  1.11.0
validatorbetween.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 "validatorbetween_p.h"
20 
21 using namespace Cutelyst;
22 
23 ValidatorBetween::ValidatorBetween(const QString &field, QMetaType::Type type, double min, double max, const QString &label, const QString &customError) :
24  ValidatorRule(*new ValidatorBetweenPrivate(field, type, min, max, label, customError))
25 {
26 }
27 
28 
29 ValidatorBetween::ValidatorBetween(ValidatorBetweenPrivate &dd) :
30  ValidatorRule(dd)
31 {
32 }
33 
34 
36 {
37 }
38 
39 
40 
42 {
43  QString result;
44 
45  const QString v = value();
46 
47  if (!v.isEmpty()) {
48  Q_D(const ValidatorBetween);
49 
50  if (d->type == QMetaType::Int) {
51  qlonglong val = v.toLongLong();
52  qlonglong min = (qlonglong)d->min;
53  qlonglong max = (qlonglong)d->max;
54  if ((val < min) || (val > max)) {
55  result = validationError();
56  }
57  } else if (d->type == QMetaType::UInt) {
58  qulonglong val = v.toULongLong();
59  qulonglong min = (qulonglong)d->min;
60  qulonglong max = (qulonglong)d->max;
61  if ((val < min) || (val > max)) {
62  result = validationError();
63  }
64  } else if (d->type == QMetaType::Float) {
65  double val = v.toDouble();
66  if ((val < d->min) || (val > d->max)) {
67  result = validationError();
68  }
69  } else if (d->type == QMetaType::QString) {
70  int val = v.length();
71  int min = (int)d->min;
72  int max = (int)d->max;
73  if ((val < min) || (val > max)) {
74  result = validationError();
75  }
76  } else {
77  result = validationDataError();
78  }
79  }
80 
81  return result;
82 }
83 
85 {
86  QString error;
87 
88  Q_D(const ValidatorBetween);
89 
90  QString min, max;
91  if (d->type == QMetaType::Int || d->type == QMetaType::UInt || d->type == QMetaType::QString) {
92  min = QString::number(d->min, 'f', 0);
93  max = QString::number(d->max, 'f', 0);
94  } else {
95  min = QString::number(d->min);
96  max = QString::number(d->max);
97  }
98 
99  if (label().isEmpty()) {
100 
101  switch (d->type) {
102  case QMetaType::Int:
103  case QMetaType::UInt:
104  case QMetaType::Float:
105  error = QStringLiteral("Value has to be between %1 and %2.").arg(min, max);
106  break;
107  case QMetaType::QString:
108  error = QStringLiteral("Length has to be between %1 and %2.").arg(min, max);
109  default:
110  error = validationDataError();
111  break;
112  }
113 
114  } else {
115 
116  switch (d->type) {
117  case QMetaType::Int:
118  case QMetaType::UInt:
119  case QMetaType::Float:
120  error = QStringLiteral("The value of the “%1” field has to be between %2 and %3.").arg(label(), min, max);
121  break;
122  case QMetaType::QString:
123  error = QStringLiteral("The length of the “%1” field has to be between %2 and %3.").arg(label(), min, max);
124  default:
125  error = validationDataError();
126  break;
127  }
128  }
129 
130  return error;
131 }
132 
133 void ValidatorBetween::setType(QMetaType::Type type)
134 {
135  Q_D(ValidatorBetween);
136  d->type = type;
137 }
138 
139 void ValidatorBetween::setMin(double min)
140 {
141  Q_D(ValidatorBetween);
142  d->min = min;
143 }
144 
145 void ValidatorBetween::setMax(double max)
146 {
147  Q_D(ValidatorBetween);
148  d->max = max;
149 }
Checks if a value or text length is between a minimum and maximum value.
void setMax(double max)
Sets the maximum value.
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...
ValidatorBetween(const QString &field, QMetaType::Type type, double min, double max, const QString &label=QString(), const QString &customError=QString())
Constructs a new between validator.
void setMin(double min)
Sets the minimum value.
QString value() const
Returns the field value.
~ValidatorBetween()
Deconstructs the between validator.
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 setType(QMetaType::Type type)
Sets the type to compare.
QString validationDataError() const
Returns an error message if any validation data is missing or invalid.
QString label() const
Returns the human readable field label used for generic error messages.
QString genericValidationError() const override
Returns a generic error message.