Cutelyst  1.11.0
validatorrequiredwith.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 "validatorrequiredwith_p.h"
20 
21 using namespace Cutelyst;
22 
23 ValidatorRequiredWith::ValidatorRequiredWith(const QString &field, const QStringList &otherFields, const QString &label, const QString &customError) :
24  ValidatorRule(*new ValidatorRequiredWithPrivate(field, otherFields, label, customError))
25 {
26 }
27 
28 ValidatorRequiredWith::ValidatorRequiredWith(ValidatorRequiredWithPrivate &dd) :
29  ValidatorRule(dd)
30 {
31 }
32 
34 {
35 }
36 
38 {
39  QString result;
40 
41  Q_D(const ValidatorRequiredWith);
42 
43  if (d->otherFields.empty()) {
44  result = validationDataError();
45  } else {
46  bool containsOther = false;
47 
48  const QStringList ofc = d->otherFields;
49 
50  for (const QString &other : ofc) {
51  if (parameters().contains(other)) {
52  containsOther = true;
53  break;
54  }
55  }
56 
57  if (containsOther && value().isEmpty()) {
58  result = validationError();
59  }
60  }
61 
62  return result;
63 }
64 
66 {
67  QString error;
68  if (label().isEmpty()) {
69  error = QStringLiteral("This is required.");
70  } else {
71  error = QStringLiteral("You must fill in the “%1” field.").arg(label());
72  }
73  return error;
74 }
75 
76 void ValidatorRequiredWith::setOtherFields(const QStringList &otherFields)
77 {
79  d->otherFields = otherFields;
80 }
QString value() const
Returns the field value.
ParamsMultiMap parameters() const
Returns the parameters to validate.
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.
~ValidatorRequiredWith()
Deconstructs the required with validator.
QString genericValidationError() const override
Returns a generic error message.
ValidatorRequiredWith(const QString &field, const QStringList &otherFields, const QString &label=QString(), const QString &customError=QString())
Constructs a new required with validator.
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...
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.
The field under validation must be present and not empty only if any of the other specified fields ar...
void setOtherFields(const QStringList &otherFields)
Sets the list of other fields.