cutelyst  5.0.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrequiredwithout.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2025 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorrequiredwithout_p.h"
7 
8 using namespace Cutelyst;
9 
11  const QStringList &otherFields,
12  const Cutelyst::ValidatorMessages &messages)
13  : ValidatorRule(*new ValidatorRequiredWithoutPrivate(field, otherFields, messages))
14 {
15 }
16 
18 
20  const ParamsMultiMap &params) const
21 {
22  ValidatorReturnType result;
23 
24  Q_D(const ValidatorRequiredWithout);
25 
26  if (d->otherFields.isEmpty()) {
28  qCWarning(C_VALIDATOR).noquote() << "Invalid validation data";
29  } else {
30  const QString v = value(params);
31 
32  auto it = std::ranges::find_if(
33  d->otherFields, [params](const QString &other) { return !params.contains(other); });
34 
35  bool otherMissing = (it != d->otherFields.end());
36  if (otherMissing) {
37  if (!v.isEmpty()) {
38  result.value.setValue(v);
39  } else {
40  result.errorMessage = validationError(c);
41  qCDebug(C_VALIDATOR).noquote().nospace()
42  << debugString(c) << " The field is not present or empty but the field \""
43  << *it << "\" is not present";
44  }
45  } else {
46  if (!v.isEmpty()) {
47  result.value.setValue(v);
48  }
49  }
50  }
51 
52  return result;
53 }
54 
56  const ParamsMultiMap &params,
57  ValidatorRtFn cb) const
58 {
59  cb(validate(c, params));
60 }
61 
63  const QVariant &errorData) const
64 {
65  // translation strings are defined in ValidatorRequired
66  Q_UNUSED(errorData)
67  const QString _label = label(c);
68  if (_label.isEmpty()) {
69  return c->qtTrId("cutelyst-validator-genvalerr-req");
70  } else {
71  return c->qtTrId("cutelyst-validator-genvalerr-req-label").arg(_label);
72  }
73 }
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Stores custom error messages and the input field label.
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
The Cutelyst Context.
Definition: context.h:42
bool isEmpty() const const
The Cutelyst namespace holds all public Cutelyst API.
QString debugString(const Context *c) const
Base class for all validator rules.
ValidatorRequiredWithout(const QString &field, const QStringList &otherFields, const ValidatorMessages &messages=ValidatorMessages())
QString value(const ParamsMultiMap &params) const
QString label(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
Definition: validatorrule.h:82
QString validationError(Context *c, const QVariant &errorData={}) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:658
The field under validation must be present and not empty only if any of the other specified fields is...
Contains the result of a single input parameter validation.
Definition: validatorrule.h:52
QString arg(Args &&... args) const const
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
void setValue(QVariant &&value)