cutelyst 4.0.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-2023 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
12const QStringList ValidatorAcceptedPrivate::trueVals{u"yes"_qs, u"on"_qs, u"1"_qs, u"true"_qs};
13
15 const Cutelyst::ValidatorMessages &messages)
16 : ValidatorRule(*new ValidatorAcceptedPrivate(field, messages))
17{
18}
19
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).noquote() << debugString(c);
33 }
34
35 return result;
36}
37
38bool ValidatorAccepted::validate(const QString &value)
39{
40 return ValidatorAcceptedPrivate::trueVals.contains(value, Qt::CaseInsensitive);
41}
42
44 const QVariant &errorData) const
45{
46 QString error;
47 Q_UNUSED(errorData)
48 const QString _label = label(c);
49 if (_label.isEmpty()) {
50 error = c->translate("Cutelyst::ValidatorAccepted", "Has to be accepted.");
51 } else {
52 //: %1 will be replaced by the field label
53 error = c->translate("Cutelyst::ValidatorAccepted", "“%1” has to be accepted.");
54 }
55 return error;
56}
The Cutelyst Context.
Definition: context.h:38
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 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.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
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