Cutelyst  1.11.0
validatorbefore.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 "validatorbefore_p.h"
20 
21 #include <QtCore/QLoggingCategory>
22 
23 using namespace Cutelyst;
24 
25 Q_LOGGING_CATEGORY(C_VALIDATORBEFORE, "cutelyst.utils.validator.before")
26 
27 ValidatorBefore::ValidatorBefore(const QString &field, const QVariant &dateTime, const QString &inputFormat, const QString &label, const QString &customError) :
28  ValidatorRule(*new ValidatorBeforePrivate(field, dateTime, inputFormat, label, customError))
29 {
30 }
31 
32 ValidatorBefore::ValidatorBefore(ValidatorBeforePrivate &dd) :
33  ValidatorRule(dd)
34 {
35 }
36 
38 {
39 }
40 
42 {
43  QString result;
44 
45  Q_D(const ValidatorBefore);
46 
47  const QString v = value();
48 
49  if (!v.isEmpty()) {
50 
51  if (d->date.type() == QVariant::Date) {
52 
53  const QDate odate = d->date.toDate();
54  if (!odate.isValid()) {
55  qCWarning(C_VALIDATORBEFORE) << "Invalid validation date.";
56  result = validationDataError();
57  } else {
58  const QDate date = d->extractDate(v, d->inputFormat);
59  if (!date.isValid()) {
60  qCWarning(C_VALIDATORBEFORE) << "Can not parse input date:" << v;
61  result = parsingError();
62  } else {
63  if (date >= odate) {
64  result = validationError();
65  }
66  }
67  }
68 
69  } else if (d->date.type() == QVariant::DateTime) {
70 
71  const QDateTime odatetime = d->date.toDateTime();
72  if (!odatetime.isValid()) {
73  qCWarning(C_VALIDATORBEFORE) << "Invalid validation date and time.";
74  result = validationDataError();
75  } else {
76  const QDateTime datetime = d->extractDateTime(v, d->inputFormat);
77  if (!datetime.isValid()) {
78  qCWarning(C_VALIDATORBEFORE) << "Can not parse input date and time:" << v;
79  result = parsingError();
80  } else {
81  if (datetime >= odatetime) {
82  result = validationError();
83  }
84  }
85  }
86 
87  } else if (d->date.type() == QVariant::Time) {
88 
89  const QTime otime = d->date.toTime();
90  if (!otime.isValid()) {
91  qCWarning(C_VALIDATORBEFORE) << "Invalid validation time.";
92  result = validationDataError();
93  } else {
94  const QTime time = d->extractTime(v, d->inputFormat);
95  if (!time.isValid()) {
96  qCWarning(C_VALIDATORBEFORE) << "Can not parse input time:" << v;
97  result = parsingError();
98  } else {
99  if (time >= otime) {
100  result = validationError();
101  }
102  }
103  }
104 
105  } else {
106  qCWarning(C_VALIDATORBEFORE) << "Invalid validation data:" << d->date;
107  result = validationDataError();
108  }
109  }
110 
111  return result;
112 }
113 
115 {
116  QString error;
117 
118  Q_D(const ValidatorBefore);
119 
120  QString compDateTime;
121 
122  switch (d->date.type()) {
123  case QVariant::Date:
124  //: date shown in validator error message
125  compDateTime = d->date.toDate().toString(QStringLiteral("dd.MM.yyyy"));
126  break;
127  case QVariant::DateTime:
128  //: date and time shown in validator error message
129  compDateTime = d->date.toDateTime().toString(QStringLiteral("dd.MM.yyyy HH:mm"));
130  break;
131  case QVariant::Time:
132  //: time shown in the validator error message
133  compDateTime = d->date.toTime().toString(QStringLiteral("dd.MM.yyyy HH:mm"));
134  default:
135  break;
136  }
137 
138  if (label().isEmpty()) {
139 
140  error = QStringLiteral("Must be before %1.").arg(compDateTime);
141 
142  } else {
143 
144  switch(d->date.type()) {
145  case QVariant::Date:
146  error = QStringLiteral("The date in the “%1” field must be before “%2”.").arg(label(), compDateTime);
147  break;
148  case QVariant::DateTime:
149  error = QStringLiteral("The date and time in the “%1” field must be before “%2”.").arg(label(), compDateTime);
150  break;
151  case QVariant::Time:
152  error = QStringLiteral("The time in the “%1” field must be before “%2”.").arg(label(), compDateTime);
153  break;
154  default:
155  error = validationDataError();
156  break;
157  }
158  }
159 
160  return error;
161 }
162 
163 void ValidatorBefore::setDateTime(const QVariant &dateTime)
164 {
165  Q_D(ValidatorBefore);
166  d->date = dateTime;
167 }
168 
169 void ValidatorBefore::setInputFormat(const QString &format)
170 {
171  Q_D(ValidatorBefore);
172  d->inputFormat = format;
173 }
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...
void setInputFormat(const QString &format)
Sets optional format for input data parsing.
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.
Checks if a date, time or datetime is before a comparison value.
ValidatorBefore(const QString &field, const QVariant &dateTime, const QString &inputFormat=QString(), const QString &label=QString(), const QString &customError=QString())
Constructs a new before validator.
~ValidatorBefore()
Deconstructs the before validator.
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.
void setDateTime(const QVariant &dateTime)
Sets the QDate, QTime or QDateTime to compare against.
QString parsingError() const
Returns an error message if an error occured while parsing input.