cutelyst  5.0.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorpresent.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2025 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorpresent_p.h"
7 
8 using namespace Cutelyst;
9 
11  const Cutelyst::ValidatorMessages &messages)
12  : ValidatorRule(*new ValidatorPresentPrivate(field, messages))
13 {
14 }
15 
17 
19 {
20  ValidatorReturnType result;
21 
22  if (!params.contains(field())) {
23  result.errorMessage = validationError(c);
24  qCDebug(C_VALIDATOR).noquote() << debugString(c) << "Field not found";
25  } else {
26  result.value.setValue<QString>(value(params));
27  }
28 
29  return result;
30 }
31 
33 {
34  cb(validate(c, params));
35 }
36 
38 {
39  Q_UNUSED(errorData)
40  const QString _label = label(c);
41  if (_label.isEmpty()) {
42  //% "Has to be present in input data."
43  return c->qtTrId("cutelyst-vapresent-genvalerr");
44  } else {
45  //: %1 will be replaced by the field label
46  //% "The “%1” field was not found in the input data."
47  return c->qtTrId("cutelyst-vapresent-genvalerr-label").arg(_label);
48  }
49 }
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
Stores custom error messages and the input field label.
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.
QString field() const noexcept
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
bool contains(const Key &key) const const
QString validationError(Context *c, const QVariant &errorData={}) const
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:658
Contains the result of a single input parameter validation.
Definition: validatorrule.h:52
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
QString arg(Args &&... args) const const
ValidatorPresent(const QString &field, const ValidatorMessages &messages={})
void setValue(QVariant &&value)