cutelyst 4.3.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;
11
12const QStringList ValidatorBooleanPrivate::trueVals{u"1"_qs, u"true"_qs, u"on"_qs};
13const QStringList ValidatorBooleanPrivate::falseVals{u"0"_qs, u"false"_qs, u"off"_qs};
14
16 const ValidatorMessages &messages,
17 const QString &defValKey)
18 : ValidatorRule(*new ValidatorBooleanPrivate(field, messages, defValKey))
19{
20}
21
23
25{
27
28 const QString v = value(params);
29
30 if (!v.isEmpty()) {
31 if (ValidatorBooleanPrivate::trueVals.contains(v, Qt::CaseInsensitive)) {
32 result.value.setValue<bool>(true);
33 } else if (ValidatorBooleanPrivate::falseVals.contains(v, Qt::CaseInsensitive)) {
34 result.value.setValue<bool>(false);
35 } else {
36 result.errorMessage = validationError(c);
37 qCDebug(C_VALIDATOR).noquote().nospace()
38 << debugString(c) << " \"" << v << "\" can not be interpreted as boolean";
39 }
40 } else {
41 defaultValue(c, &result);
42 }
43
44 return result;
45}
46
48 const QVariant &errorData) const
49{
50 Q_UNUSED(errorData)
51 const QString _label = label(c);
52 if (_label.isEmpty()) {
53 //% "Can not be interpreted as boolean value."
54 return c->qtTrId("cutelyst-vaboolean-genvalerr");
55 } else {
56 //: %1 will be replaced by the field label
57 //% "The value in the “%1” field can not be interpreted as a boolean value."
58 return c->qtTrId("cutelyst-vaboolean-genvalerr-label").arg(_label);
59 }
60}
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
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
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.