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