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