cutelyst 4.8.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorboolean.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorboolean_p.h"
7
8#include <QStringList>
9
10using namespace Cutelyst;
11using namespace Qt::Literals::StringLiterals;
12
13const QStringList ValidatorBooleanPrivate::trueVals{u"1"_s, u"true"_s, u"on"_s};
14const QStringList ValidatorBooleanPrivate::falseVals{u"0"_s, u"false"_s, u"off"_s};
15
17 const ValidatorMessages &messages,
18 const QString &defValKey)
19 : ValidatorRule(*new ValidatorBooleanPrivate(field, messages, defValKey))
20{
21}
22
24
26{
28
29 const QString v = value(params);
30
31 if (!v.isEmpty()) {
32 if (ValidatorBooleanPrivate::trueVals.contains(v, Qt::CaseInsensitive)) {
33 result.value.setValue<bool>(true);
34 } else if (ValidatorBooleanPrivate::falseVals.contains(v, Qt::CaseInsensitive)) {
35 result.value.setValue<bool>(false);
36 } else {
37 result.errorMessage = validationError(c);
38 qCDebug(C_VALIDATOR).noquote().nospace()
39 << debugString(c) << " \"" << v << "\" can not be interpreted as boolean";
40 }
41 } else {
42 defaultValue(c, &result);
43 }
44
45 return result;
46}
47
49 const QVariant &errorData) const
50{
51 Q_UNUSED(errorData)
52 const QString _label = label(c);
53 if (_label.isEmpty()) {
54 //% "Can not be interpreted as boolean value."
55 return c->qtTrId("cutelyst-vaboolean-genvalerr");
56 } else {
57 //: %1 will be replaced by the field label
58 //% "The value in the “%1” field can not be interpreted as a boolean value."
59 return c->qtTrId("cutelyst-vaboolean-genvalerr-label").arg(_label);
60 }
61}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:656
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorBoolean(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
QString field() const noexcept
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
ValidatorRule(const QString &field, const ValidatorMessages &messages={}, const QString &defValKey={}, QByteArrayView validatorName=nullptr)
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
QMultiMap< QString, QString > ParamsMultiMap
The Cutelyst namespace holds all public Cutelyst API.
QString arg(Args &&... args) const const
bool isEmpty() const const
CaseInsensitive
void setValue(QVariant &&value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.