cutelyst 4.0.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 QString error;
51 Q_UNUSED(errorData)
52 const QString _label = label(c);
53 if (_label.isEmpty()) {
54 error =
55 c->translate("Cutelyst::ValidatorBoolean", "Can not be interpreted as boolean value.");
56 } else {
57 //: %1 will be replaced by the field label
58 error =
59 c->translate("Cutelyst::ValidatorBoolean",
60 "The value in the “%1” field can not be interpreted as a boolean value.")
61 .arg(_label);
62 }
63 return error;
64}
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
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
~ValidatorBoolean() override
Deconstructs the validator.
ValidatorBoolean(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new validator.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
void defaultValue(Context *c, ValidatorReturnType *result) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...
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.
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