cutelyst  3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatoraccepted.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatoraccepted_p.h"
7 
8 #include <QStringList>
9 
10 using namespace Cutelyst;
11 
13  const Cutelyst::ValidatorMessages &messages)
14  : ValidatorRule(*new ValidatorAcceptedPrivate(field, messages))
15 {
16 }
17 
19 {
20 }
21 
23  const Cutelyst::ParamsMultiMap &params) const
24 {
25  ValidatorReturnType result;
26 
27  if (Q_LIKELY(ValidatorAccepted::validate(value(params)))) {
28  result.value.setValue<bool>(true);
29  } else {
30  result.errorMessage = validationError(c);
31  result.value.setValue<bool>(false);
32  qCDebug(C_VALIDATOR,
33  "ValidatorAccepted: Validation failed for field %s at %s::%s.",
34  qPrintable(field()),
35  qPrintable(c->controllerName()),
36  qPrintable(c->actionName()));
37  }
38 
39  return result;
40 }
41 
43 {
44  bool ret = true;
45  static const QStringList l(
46  {QStringLiteral("yes"), QStringLiteral("on"), QStringLiteral("1"), QStringLiteral("true")});
48  return ret;
49 }
50 
52  const QVariant &errorData) const
53 {
54  QString error;
55  Q_UNUSED(errorData)
56  const QString _label = label(c);
57  if (_label.isEmpty()) {
58  error = c->translate("Cutelyst::ValidatorAccepted", "Has to be accepted.");
59  } else {
60  //: %1 will be replaced by the field label
61  error = c->translate("Cutelyst::ValidatorAccepted", "“%1” has to be accepted.");
62  }
63  return error;
64 }
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
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.
~ValidatorAccepted() override
Deconstructs the accepted validator.
The Cutelyst Context.
Definition: context.h:38
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Creates a generic error message.
CaseInsensitive
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:490
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.
static bool validate(const QString &value)
Returns true if the value is yes, on, 1, or true.
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
ValidatorAccepted(const QString &field, const ValidatorMessages &messages=ValidatorMessages())
Constructs a new accepted validator.
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
void setValue(QVariant &&value)