Cutelyst  1.11.0
validatormax.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 "validatormax_p.h"
20 
21 using namespace Cutelyst;
22 
23 ValidatorMax::ValidatorMax(const QString &field, QMetaType::Type type, double max, const QString &label, const QString &customError) :
24  ValidatorRule(*new ValidatorMaxPrivate(field, type, max, label, customError))
25 {
26 }
27 
28 ValidatorMax::ValidatorMax(ValidatorMaxPrivate &dd) :
29  ValidatorRule(dd)
30 {
31 }
32 
34 {
35 }
36 
37 QString ValidatorMax::validate() const
38 {
39  QString result;
40 
41  const QString v = value();
42 
43  if (!v.isEmpty()) {
44  Q_D(const ValidatorMax);
45 
46  if (d->type == QMetaType::Int) {
47  qlonglong val = v.toLongLong();
48  qlonglong max = (qlonglong)d->max;
49  if (val > max) {
50  result = validationError();
51  }
52  } else if (d->type == QMetaType::UInt) {
53  qulonglong val = v.toULongLong();
54  qulonglong max = (qulonglong)d->max;
55  if (val > max) {
56  result = validationError();
57  }
58  } else if (d->type == QMetaType::Float) {
59  double val = v.toDouble();
60  if (val > d->max) {
61  result = validationError();
62  }
63  } else if (d->type == QMetaType::QString) {
64  int val = v.length();
65  int max = (int)d->max;
66  if (val > max) {
67  result = validationError();
68  }
69  } else {
70  result = validationDataError();
71  }
72  }
73 
74  return result;
75 }
76 
78 {
79  QString error;
80 
81  Q_D(const ValidatorMax);
82 
83  QString max;
84  if (d->type == QMetaType::Int || d->type == QMetaType::UInt || d->type == QMetaType::QString) {
85  max = QString::number(d->max, 'f', 0);
86  } else {
87  max = QString::number(d->max);
88  }
89 
90  if (label().isEmpty()) {
91  if (d->type == QMetaType::Int || d->type == QMetaType::UInt || d->type == QMetaType::Float) {
92  error = QStringLiteral("Has to be lower than or equal to %1.").arg(max);
93  } else if (d->type == QMetaType::QString) {
94  error = QStringLiteral("Has to be shorter than or equal to %1.").arg(max);
95  } else {
96  error = validationDataError();
97  }
98  } else {
99  if (d->type == QMetaType::Int || d->type == QMetaType::UInt || d->type == QMetaType::Float) {
100  error = QStringLiteral("The value of the “%1” field has to be lower than or equal to %2.").arg(label(), max);
101  } else if (d->type == QMetaType::QString) {
102  error = QStringLiteral("The length of the “%1” field has to be shorter than or equal to %2.").arg(label(), max);
103  } else {
104  error = validationDataError();
105  }
106  }
107 
108  return error;
109 }
110 
111 void ValidatorMax::setType(QMetaType::Type type)
112 {
113  Q_D(ValidatorMax);
114  d->type = type;
115 }
116 
117 void ValidatorMax::setMax(double max)
118 {
119  Q_D(ValidatorMax);
120  d->max = max;
121 }
void setMax(double max)
Sets the maximum value.
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...
ValidatorMax(const QString &field, QMetaType::Type type, double max, const QString &label=QString(), const QString &customError=QString())
Constructs a new max 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.
QString genericValidationError() const override
Returns a generic error message.
void setType(QMetaType::Type type)
Sets the type to validate.
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.
Checks if a value is not bigger or longer than a maximum value.
Definition: validatormax.h:50
~ValidatorMax()
Deconstructs the max validator.