Cutelyst  1.11.0
validatorrequiredunless.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 "validatorrequiredunless_p.h"
20 
21 using namespace Cutelyst;
22 
23 ValidatorRequiredUnless::ValidatorRequiredUnless(const QString &field, const QString &otherField, const QStringList &otherValues, const QString &label, const QString &customError) :
24  ValidatorRule(*new ValidatorRequiredUnlessPrivate(field, otherField, otherValues, label, customError))
25 {
26 }
27 
28 ValidatorRequiredUnless::ValidatorRequiredUnless(ValidatorRequiredUnlessPrivate &dd) :
29  ValidatorRule(dd)
30 {
31 }
32 
34 {
35 }
36 
38 {
39  QString result;
40 
41  Q_D(const ValidatorRequiredUnless);
42 
43  if (d->otherField.isEmpty() || d->otherValues.empty()) {
44  result = validationDataError();
45  } else if (!d->otherValues.contains(d->parameters.value(d->otherField)) && value().isEmpty()) {
46  result = validationError();
47  }
48 
49  return result;
50 }
51 
53 {
54  QString error;
55  if (label().isEmpty()) {
56  error = QStringLiteral("This is required.");
57  } else {
58  error = QStringLiteral("You must fill in the “%1” field.").arg(label());
59  }
60  return error;
61 }
62 
63 void ValidatorRequiredUnless::setOtherField(const QString &otherField)
64 {
66  d->otherField = otherField;
67 }
68 
69 void ValidatorRequiredUnless::setOtherValues(const QStringList &otherValues)
70 {
72  d->otherValues = otherValues;
73 }
The field under validation must be present and not empty unless the other field is equal to any value...
QString value() const
Returns the field value.
void setOtherField(const QString &otherField)
Sets the name of the other field.
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...
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 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.
~ValidatorRequiredUnless()
Deconstructs the required unless validator.
void setOtherValues(const QStringList &otherValues)
Sets the comparative values for the other field.
QString genericValidationError() const override
Returns a generic error message.
ValidatorRequiredUnless(const QString &field, const QString &otherField, const QStringList &otherValues, const QString &label=QString(), const QString &customError=QString())
Constructs a new required unless validator.