cutelyst  3.7.0
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 #include <QStringList>
8 
9 using namespace Cutelyst;
10 
11 ValidatorAccepted::ValidatorAccepted(const QString &field, const Cutelyst::ValidatorMessages &messages) :
12  ValidatorRule(*new ValidatorAcceptedPrivate(field, messages))
13 {
14 
15 }
16 
18 {
19 
20 }
21 
23 {
24  ValidatorReturnType result;
25 
26  if (Q_LIKELY(ValidatorAccepted::validate(value(params)))) {
27  result.value.setValue<bool>(true);
28  } else {
29  result.errorMessage = validationError(c);
30  result.value.setValue<bool>(false);
31  qCDebug(C_VALIDATOR, "ValidatorAccepted: Validation failed for field %s at %s::%s.", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
32  }
33 
34  return result;
35 }
36 
37 bool ValidatorAccepted::validate(const QString &value)
38 {
39  bool ret = true;
40  static const QStringList l({QStringLiteral("yes"), QStringLiteral("on"), QStringLiteral("1"), QStringLiteral("true")});
41  ret = l.contains(value, Qt::CaseInsensitive);
42  return ret;
43 }
44 
45 QString ValidatorAccepted::genericValidationError(Cutelyst::Context *c, const QVariant &errorData) const
46 {
47  QString error;
48  Q_UNUSED(errorData)
49  const QString _label = label(c);
50  if (_label.isEmpty()) {
51  error = c->translate("Cutelyst::ValidatorAccepted", "Has to be accepted.");
52  } else {
53  //: %1 will be replaced by the field label
54  error = c->translate("Cutelyst::ValidatorAccepted", "“%1” has to be accepted.");
55  }
56  return error;
57 }
The Cutelyst Context.
Definition: context.h:39
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
ValidatorAccepted(const QString &field, const ValidatorMessages &messages=ValidatorMessages())
Constructs a new accepted validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Creates a generic error message.
~ValidatorAccepted() override
Deconstructs the accepted validator.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString field() const
Returns the name of the field to validate.
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
static bool validate(const QString &value)
Returns true if the value is yes, on, 1, or true.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49