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