cutelyst  3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrequiredwithall.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorrequiredwithall_p.h"
7 
8 using namespace Cutelyst;
9 
11  const QStringList &otherFields,
12  const ValidatorMessages &messages)
13  : ValidatorRule(*new ValidatorRequiredWithAllPrivate(field, otherFields, messages))
14 {
15 }
16 
18 {
19 }
20 
22  const ParamsMultiMap &params) const
23 {
24  ValidatorReturnType result;
25 
26  Q_D(const ValidatorRequiredWithAll);
27 
28  if (d->otherFields.empty()) {
30  qCWarning(C_VALIDATOR,
31  "ValidatorRequiredWithAll: invalid validation data for field %s at %s::%s",
32  qPrintable(field()),
33  qPrintable(c->controllerName()),
34  qPrintable(c->actionName()));
35  } else {
36 
37  bool containsAll = true;
38 
39  const QStringList ofc = d->otherFields;
40 
41  for (const QString &other : ofc) {
42  if (!params.contains(other)) {
43  containsAll = false;
44  break;
45  }
46  }
47 
48  const QString v = value(params);
49 
50  if (containsAll) {
51  if (!v.isEmpty()) {
52  result.value.setValue(v);
53  } else {
54  result.errorMessage = validationError(c);
55  qCDebug(C_VALIDATOR,
56  "ValidatorRequiredWithAll: Validation failed for field %s at %s::%s",
57  qPrintable(field()),
58  qPrintable(c->controllerName()),
59  qPrintable(c->actionName()));
60  }
61  } else {
62  if (!v.isEmpty()) {
63  result.value.setValue(v);
64  }
65  }
66  }
67 
68  return result;
69 }
70 
72  const QVariant &errorData) const
73 {
74  QString error;
75  const QString _label = label(c);
76  Q_UNUSED(errorData)
77  if (_label.isEmpty()) {
78  error = c->translate("Cutelyst::ValidatorRequiredWithAll", "This is required.");
79  } else {
80  //: %1 will be replaced by the field label
81  error = c->translate("Cutelyst::ValidatorRequiredWithAll", "The “%1” field is required.")
82  .arg(_label);
83  }
84  return error;
85 }
~ValidatorRequiredWithAll() override
Deconstructs the required with all validator.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Stores custom error messages and the input field label.
<Cutelyst/Plugins/Utils/validatorrequiredwithall.h>
The Cutelyst Context.
Definition: context.h:38
ValidatorRequiredWithAll(const QString &field, const QStringList &otherFields, const ValidatorMessages &messages=ValidatorMessages())
Constructs a new required with all validator.
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:490
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
bool contains(const Key &key, const T &value) const const
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49