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