Cutelyst  1.11.0
validatorboolean.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 "validatorboolean_p.h"
20 #include <QStringList>
21 
22 using namespace Cutelyst;
23 
24 ValidatorBoolean::ValidatorBoolean(const QString &field, const QString &label, const QString &customError) :
25  ValidatorRule(*new ValidatorBooleanPrivate(field, label, customError))
26 {
27 }
28 
29 ValidatorBoolean::ValidatorBoolean(ValidatorBooleanPrivate &dd) :
30  ValidatorRule(dd)
31 {
32 }
33 
35 {
36 }
37 
39 {
40  QString result;
41 
42  if (!value().isEmpty()) {
43  static const QStringList l({QStringLiteral("1"), QStringLiteral("0"), QStringLiteral("true"), QStringLiteral("false"), QStringLiteral("on"), QStringLiteral("off")});
44  if (!l.contains(value(), Qt::CaseInsensitive)) {
45  result = validationError();
46  }
47  }
48 
49  return result;
50 }
51 
53 {
54  QString error;
55  if (label().isEmpty()) {
56  error = QStringLiteral("Can not be interpreted as boolean.");
57  } else {
58  error = QStringLiteral("The data in the “%1” field can not be interpreted as a boolean.").arg(label());
59  }
60  return error;
61 }
62 
QString genericValidationError() const override
Returns a generic error message.
ValidatorBoolean(const QString &field, const QString &label=QString(), const QString &customError=QString())
Constructs a new validator.
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.
QString label() const
Returns the human readable field label used for generic error messages.
~ValidatorBoolean()
Deconstructs the validator.
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...