cutelyst 5.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-2025 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;
11using namespace Qt::Literals::StringLiterals;
12
13const QStringList ValidatorAcceptedPrivate::trueVals{u"yes"_s, u"on"_s, u"1"_s, u"true"_s};
14
16 const Cutelyst::ValidatorMessages &messages)
17 : ValidatorRule(*new ValidatorAcceptedPrivate(field, messages))
18{
19}
20
22
24 const Cutelyst::ParamsMultiMap &params) const
25{
27
28 if (Q_LIKELY(ValidatorAccepted::validate(value(params)))) {
29 result.value.setValue<bool>(true);
30 } else {
31 result.errorMessage = validationError(c);
32 result.value.setValue<bool>(false);
33 qCDebug(C_VALIDATOR).noquote() << debugString(c);
34 }
35
36 return result;
37}
38
40{
41 if (Q_LIKELY(ValidatorAccepted::validate(value(params)))) {
42 cb({.errorMessage = {}, .value = true});
43 } else {
44 qCDebug(C_VALIDATOR).noquote() << debugString(c);
45 cb({.errorMessage = validationError(c), .value = false});
46 }
47}
48
49bool ValidatorAccepted::validate(const QString &value)
50{
51 return ValidatorAcceptedPrivate::trueVals.contains(value, Qt::CaseInsensitive);
52}
53
55 const QVariant &errorData) const
56{
57 Q_UNUSED(errorData)
58 const QString _label = label(c);
59 if (_label.isEmpty()) {
60 //% "Has to be accepted."
61 return c->qtTrId("cutelyst-valaccepted-genvalerr");
62 } else {
63 //: %1 will be replaced by the field label
64 //% "“%1” has to be accepted."
65 return c->qtTrId("cutelyst-valaccepted-genvalerr-label").arg(_label);
66 }
67}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:657
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorAccepted(const QString &field, const ValidatorMessages &messages={})
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(const Context *c) const
QString debugString(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
QString value(const ParamsMultiMap &params) const
QMultiMap< QString, QString > ParamsMultiMap
static bool validate(const QString &value)
Returns true if the value is equal to yes, on, 1, or true.
The Cutelyst namespace holds all public Cutelyst API.
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.