Cutelyst  3.5.0
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 
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 
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 
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 }
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.
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
~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.
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:471
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.
void setValue(const T &value)
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