cutelyst  5.0.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrequired.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2025 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorrequired_p.h"
7 
8 using namespace Cutelyst;
9 
11  const Cutelyst::ValidatorMessages &messages)
12  : ValidatorRule(*new ValidatorRequiredPrivate(field, messages))
13 {
14 }
15 
17 
19  const Cutelyst::ParamsMultiMap &params) const
20 {
21  ValidatorReturnType result;
22 
23  if (!params.contains(field())) {
24  qCDebug(C_VALIDATOR).noquote() << debugString(c) << "Field not found";
25  result.errorMessage = validationError(c);
26  return result;
27  }
28 
29  const QString v = value(params);
30  if (Q_LIKELY(!v.isEmpty())) {
31  result.value.setValue(v);
32  } else {
33  qCDebug(C_VALIDATOR).noquote() << debugString(c) << "The field is empty";
34  result.errorMessage = validationError(c);
35  }
36 
37  return result;
38 }
39 
41 {
42  cb(validate(c, params));
43 }
44 
46  const QVariant &errorData) const
47 {
48  Q_UNUSED(errorData)
49  const QString _label = label(c);
50  if (_label.isEmpty()) {
51  //% "This field is required."
52  return c->qtTrId("cutelyst-validator-genvalerr-req");
53  } else {
54  //: %1 will be replaced by the field label
55  //% "The “%1” field is required."
56  return c->qtTrId("cutelyst-validator-genvalerr-req-label").arg(_label);
57  }
58 }
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) 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
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:658
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
Contains the result of a single input parameter validation.
Definition: validatorrule.h:52
ValidatorRequired(const QString &field, const ValidatorMessages &messages={})
QString arg(Args &&... args) const const
void setValue(QVariant &&value)