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
10using namespace Cutelyst;
11
13 const Cutelyst::ValidatorMessages &messages)
14 : ValidatorRule(*new ValidatorAcceptedPrivate(field, messages))
15{
16}
17
21
23 const Cutelyst::ParamsMultiMap &params) const
24{
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}
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:490
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.
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.
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
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
bool isEmpty() const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
CaseInsensitive
void setValue(const T &value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.