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