Cutelyst  1.11.0
validatordatetime.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 "validatordatetime_p.h"
20 
21 #include <QDateTime>
22 
23 using namespace Cutelyst;
24 
25 ValidatorDateTime::ValidatorDateTime(const QString &field, const QString &format, const QString &label, const QString &customError) :
26  ValidatorRule(*new ValidatorDateTimePrivate(field, format, label, customError))
27 {
28 }
29 
30 ValidatorDateTime::ValidatorDateTime(ValidatorDateTimePrivate &dd) :
31  ValidatorRule(dd)
32 {
33 }
34 
35 
37 {
38 }
39 
41 {
42  QString result;
43 
44  Q_D(const ValidatorDateTime);
45 
46  const QString v = value().trimmed();
47 
48  if (!v.isEmpty()) {
49  const QDateTime dt = d->extractDateTime(v, d->format);
50 
51  if (!dt.isValid()) {
52  result = validationError();
53  }
54  }
55 
56  return result;
57 }
58 
60 {
61  QString error;
62 
63  Q_D(const ValidatorDateTime);
64 
65  if (label().isEmpty()) {
66 
67  error = QStringLiteral("Not a valid date and time.");
68 
69  } else {
70 
71  if (!d->format.isEmpty()) {
72  error = QStringLiteral("The data in the “%1” field can not be interpreted as date and time of this schema: “%2”").arg(label(), d->format);
73  } else {
74  error = QStringLiteral("The data in the “%1” field can not be interpreted as date and time.").arg(label());
75  }
76  }
77 
78  return error;
79 }
80 
81 void ValidatorDateTime::setFormat(const QString &format)
82 {
83  Q_D(ValidatorDateTime);
84  d->format = format;
85 }
Checks if the input data is a valid datetime.
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...
void setFormat(const QString &format)
Sets an optional date format.
QString genericValidationError() const override
Returns a generic error message.
QString value() const
Returns the field value.
ValidatorDateTime(const QString &field, const QString &format=QString(), const QString &label=QString(), const QString &customError=QString())
Constructs a new datetime 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 label() const
Returns the human readable field label used for generic error messages.
~ValidatorDateTime()
Deconstructs the datetime validator.