cutelyst
4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatordatetime.cpp
1
/*
2
* SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3
* SPDX-License-Identifier: BSD-3-Clause
4
*/
5
6
#include "validatordatetime_p.h"
7
8
#include <QDateTime>
9
10
using namespace
Cutelyst
;
11
12
ValidatorDateTime::ValidatorDateTime
(
const
QString
&field,
13
const
QString
&timeZone,
14
const
char
*inputFormat,
15
const
ValidatorMessages
&messages,
16
const
QString
&defValKey)
17
:
ValidatorRule
(
18
*new ValidatorDateTimePrivate(field, timeZone, inputFormat, messages, defValKey))
19
{
20
}
21
22
ValidatorDateTime::~ValidatorDateTime
() =
default
;
23
24
ValidatorReturnType
ValidatorDateTime::validate
(
Context
*c,
const
ParamsMultiMap
¶ms)
const
25
{
26
ValidatorReturnType
result;
27
28
Q_D(
const
ValidatorDateTime
);
29
30
const
QString
v =
value
(params);
31
32
if
(!v.
isEmpty
()) {
33
const
QTimeZone
tz = d->extractTimeZone(c, params, d->timeZone);
34
const
QDateTime
dt = d->extractDateTime(c, v, d->inputFormat, tz);
35
36
if
(!dt.
isValid
()) {
37
result.
errorMessage
=
validationError
(c);
38
qCDebug(C_VALIDATOR).noquote().nospace()
39
<<
debugString
(c) <<
" \""
<< v <<
"\" is not a valid datetime"
;
40
}
else
{
41
result.
value
.
setValue
(dt);
42
}
43
44
}
else
{
45
defaultValue
(c, &result);
46
}
47
48
return
result;
49
}
50
51
QString
ValidatorDateTime::genericValidationError
(
Context
*c,
const
QVariant
&errorData)
const
52
{
53
Q_D(
const
ValidatorDateTime
);
54
Q_UNUSED(errorData)
55
56
const
QString
_label =
label
(c);
57
58
if
(d->inputFormat) {
59
const
QString
inputFormatTranslated =
60
d->translationContext ? c->
translate
(d->translationContext, d->inputFormat)
61
: c->
qtTrId
(d->inputFormat);
62
if
(_label.
isEmpty
()) {
63
//: %1 will be replaced by the required date and time format
64
//% "Not a valid date and time according to the following format: %1"
65
return
c->
qtTrId
(
"cutelyst-valdatetime-genvalerr-format"
).
arg
(inputFormatTranslated);
66
}
else
{
67
//: %1 will be replaced by the field label, %2 will be replaced by
68
//: the required datetime format
69
//% "The value in the “%1” field can not be parsed as date and time "
70
//% "according to the following date and time format: %2"
71
return
c->
qtTrId
(
"cutelyst-valdatetime-genvalerr-format-label"
)
72
.
arg
(_label, inputFormatTranslated);
73
}
74
}
else
{
75
if
(_label.
isEmpty
()) {
76
//% "Not a valid date and time."
77
return
c->
qtTrId
(
"cutelyst-valdatetime-genvalerr"
);
78
}
else
{
79
//: %1 will be replaced by the field label
80
//% "The value in the “%1” field can not be parsed as date and time."
81
return
c->
qtTrId
(
"cutelyst-valdatetime-genvalerr-label"
).
arg
(_label);
82
}
83
}
84
}
Cutelyst::Context
The Cutelyst Context.
Definition
context.h:42
Cutelyst::Context::translate
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition
context.cpp:484
Cutelyst::Context::qtTrId
QString qtTrId(const char *id, int n=-1) const
Definition
context.h:656
Cutelyst::ValidatorDateTime
Checks if the input data is a valid datetime.
Definition
validatordatetime.h:52
Cutelyst::ValidatorDateTime::ValidatorDateTime
ValidatorDateTime(const QString &field, const QString &timeZone, const char *inputFormat=nullptr, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Definition
validatordatetime.cpp:12
Cutelyst::ValidatorDateTime::~ValidatorDateTime
~ValidatorDateTime() override
Cutelyst::ValidatorDateTime::validate
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Definition
validatordatetime.cpp:24
Cutelyst::ValidatorDateTime::genericValidationError
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
Definition
validatordatetime.cpp:51
Cutelyst::ValidatorRule
Base class for all validator rules.
Definition
validatorrule.h:304
Cutelyst::ValidatorRule::validationError
QString validationError(Context *c, const QVariant &errorData={}) const
Definition
validatorrule.cpp:61
Cutelyst::ValidatorRule::label
QString label(Context *c) const
Definition
validatorrule.cpp:49
Cutelyst::ValidatorRule::defaultValue
void defaultValue(Context *c, ValidatorReturnType *result) const
Definition
validatorrule.cpp:144
Cutelyst::ValidatorRule::value
QString value(const ParamsMultiMap ¶ms) const
Definition
validatorrule.cpp:34
Cutelyst::ValidatorRule::debugString
QString debugString(Context *c) const
Definition
validatorrule.cpp:157
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition
group-core-actions.dox:1
QDateTime
QDateTime::isValid
bool isValid() const const
QMultiMap
QString
QString::arg
QString arg(Args &&... args) const const
QString::isEmpty
bool isEmpty() const const
QTimeZone
QVariant
QVariant::setValue
void setValue(QVariant &&value)
Cutelyst::ValidatorMessages
Stores custom error messages and the input field label.
Definition
validatorrule.h:142
Cutelyst::ValidatorReturnType
Contains the result of a single input parameter validation.
Definition
validatorrule.h:49
Cutelyst::ValidatorReturnType::value
QVariant value
Definition
validatorrule.h:54
Cutelyst::ValidatorReturnType::errorMessage
QString errorMessage
Definition
validatorrule.h:50
Cutelyst
Plugins
Utils
Validator
validatordatetime.cpp
Generated by
1.9.8