Cutelyst  1.11.0
validatormin.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 "validatormin_p.h"
20 
21 using namespace Cutelyst;
22 
23 ValidatorMin::ValidatorMin(const QString &field, QMetaType::Type type, double min, const QString &label, const QString &customError) :
24  ValidatorRule(*new ValidatorMinPrivate(field, type, min, label, customError))
25 {
26 }
27 
28 ValidatorMin::ValidatorMin(ValidatorMinPrivate &dd) :
29  ValidatorRule(dd)
30 {
31 }
32 
34 {
35 }
36 
37 QString ValidatorMin::validate() const
38 {
39  QString result;
40 
41  const QString v = value();
42 
43  if (!v.isEmpty()) {
44  Q_D(const ValidatorMin);
45 
46  if (d->type == QMetaType::Int) {
47  qlonglong val = v.toLongLong();
48  qlonglong min = (qlonglong)d->min;
49  if (val < min) {
50  result = validationError();
51  }
52  } else if (d->type == QMetaType::UInt) {
53  qulonglong val = v.toULongLong();
54  qulonglong min = (qulonglong)d->min;
55  if (val < min) {
56  result = validationError();
57  }
58  } else if (d->type == QMetaType::Float) {
59  double val = v.toDouble();
60  if (val < d->min) {
61  result = validationError();
62  }
63  } else if (d->type == QMetaType::QString) {
64  int val = v.length();
65  int min = (int)d->min;
66  if (val < min) {
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 ValidatorMin);
82 
83  QString min;
84  if (d->type == QMetaType::Int || d->type == QMetaType::UInt || d->type == QMetaType::QString) {
85  min = QString::number(d->min, 'f', 0);
86  } else {
87  min = QString::number(d->min);
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 greater than or equal to %1.").arg(min);
93  } else if (d->type == QMetaType::QString) {
94  error = QStringLiteral("Has to be longer than or equal to %1.").arg(min);
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 greater than or equal to %2.").arg(label(), min);
101  } else if (d->type == QMetaType::QString) {
102  error = QStringLiteral("The length of the “%1” field has to be longer than or equal to %2.").arg(label(), min);
103  } else {
104  error = validationDataError();
105  }
106  }
107 
108  return error;
109 }
110 
111 void ValidatorMin::setType(QMetaType::Type type)
112 {
113  Q_D(ValidatorMin);
114  d->type = type;
115 }
116 
117 void ValidatorMin::setMin(double min)
118 {
119  Q_D(ValidatorMin);
120  d->min = min;
121 }
void setMin(double min)
Sets the maximum value.
void setType(QMetaType::Type type)
Sets the type to validate.
ValidatorMin(const QString &field, QMetaType::Type type, double min, const QString &label=QString(), const QString &customError=QString())
Constructs a new min validator.
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...
QString value() const
Returns the field value.
~ValidatorMin()
Deconstructs the min validator.
QString validationError() const
Returns a descriptive error message if validation failed.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
QString genericValidationError() const override
Returns a generic error message.
Base class for all validators.
Checks if a value is not smaller or shorter than a maximum value.
Definition: validatormin.h:50
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.