Cutelyst  1.11.0
validatornotin.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 "validatornotin_p.h"
20 
21 using namespace Cutelyst;
22 
23 ValidatorNotIn::ValidatorNotIn(const QString &field, const QStringList &values, const QString &label, const QString &customError) :
24  ValidatorRule(*new ValidatorNotInPrivate(field, values, label, customError))
25 {
26 }
27 
28 ValidatorNotIn::ValidatorNotIn(ValidatorNotInPrivate &dd) :
29  ValidatorRule(dd)
30 {
31 }
32 
34 {
35 }
36 
37 QString ValidatorNotIn::validate() const
38 {
39  QString result;
40 
41  Q_D(const ValidatorNotIn);
42 
43  if (d->values.empty()) {
44  result = validationDataError();
45  } else {
46  const QString v = value();
47  if (!v.isEmpty() && d->values.contains(v)) {
48  result = validationError();
49  }
50  }
51 
52  return result;
53 }
54 
56 {
57  QString error;
58  if (label().isEmpty()) {
59  error = QStringLiteral("Value is not allowed.");
60  } else {
61  error = QStringLiteral("The value in the “%1” field is not allowed.").arg(label());
62  }
63  return error;
64 }
65 
66 void ValidatorNotIn::setValues(const QStringList &values)
67 {
68  Q_D(ValidatorNotIn);
69  d->values = values;
70 }
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...
QString value() const
Returns the field value.
~ValidatorNotIn()
Deconstructs the 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.
ValidatorNotIn(const QString &field, const QStringList &values, const QString &label=QString(), const QString &customError=QString())
Constructs a new not in validator.
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 setValues(const QStringList &values)
Sets the values to compare against.
QString genericValidationError() const override
Returns a generic error message.
Checks if the field value is not one from a list of values.