cutelyst
4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatortime.cpp
1
/*
2
* SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3
* SPDX-License-Identifier: BSD-3-Clause
4
*/
5
6
#include "validatortime_p.h"
7
8
#include <QTime>
9
10
using namespace
Cutelyst
;
11
12
ValidatorTime::ValidatorTime
(
const
QString
&field,
13
const
char
*format,
14
const
Cutelyst::ValidatorMessages
&messages,
15
const
QString
&defValKey)
16
:
ValidatorRule
(*new ValidatorTimePrivate(field, format, messages, defValKey))
17
{
18
}
19
20
ValidatorTime::~ValidatorTime
() =
default
;
21
22
ValidatorReturnType
ValidatorTime::validate
(
Context
*c,
const
ParamsMultiMap
¶ms)
const
23
{
24
ValidatorReturnType
result;
25
26
Q_D(
const
ValidatorTime
);
27
28
const
QString
v =
value
(params);
29
30
if
(!v.
isEmpty
()) {
31
const
QTime
time = d->extractTime(c, v, d->format);
32
33
if
(!time.
isValid
()) {
34
result.
errorMessage
=
validationError
(c);
35
qCDebug(C_VALIDATOR).noquote().nospace()
36
<<
debugString
(c) <<
" \""
<< v <<
"\" is not a valid time"
;
37
}
else
{
38
result.
value
.
setValue
(time);
39
}
40
41
}
else
{
42
defaultValue
(c, &result);
43
}
44
45
return
result;
46
}
47
48
QString
ValidatorTime::genericValidationError
(
Context
*c,
const
QVariant
&errorData)
const
49
{
50
Q_D(
const
ValidatorTime
);
51
Q_UNUSED(errorData)
52
const
QString
_label =
label
(c);
53
54
if
(d->format) {
55
const
QString
_formatTranslated = d->translationContext
56
? c->
translate
(d->translationContext, d->format)
57
: c->
qtTrId
(d->format);
58
if
(_label.
isEmpty
()) {
59
//% "Not a valid time according to the following format: %1"
60
return
c->
qtTrId
(
"cutelyst-valtime-genvalerr-format"
).
arg
(_formatTranslated);
61
}
else
{
62
//% "The value in the “%1” field can not be parsed as time according "
63
//% "to the following format: %2"
64
return
c->
qtTrId
(
"cutelyst-valtime-genvalerr-format-label"
)
65
.
arg
(_label, _formatTranslated);
66
}
67
}
else
{
68
if
(_label.
isEmpty
()) {
69
//% "Not a valid time."
70
return
c->
qtTrId
(
"cutelyst-valtime-genvalerr"
);
71
}
else
{
72
//% "The value in the “%1” field can not be parsed as time."
73
return
c->
qtTrId
(
"cutelyst-valtime-genvalerr-label"
).
arg
(_label);
74
}
75
}
76
}
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::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::ValidatorTime
Checks if the input data is a valid time.
Definition
validatortime.h:44
Cutelyst::ValidatorTime::genericValidationError
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Definition
validatortime.cpp:48
Cutelyst::ValidatorTime::ValidatorTime
ValidatorTime(const QString &field, const char *format=nullptr, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Definition
validatortime.cpp:12
Cutelyst::ValidatorTime::validate
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Definition
validatortime.cpp:22
Cutelyst::ValidatorTime::~ValidatorTime
~ValidatorTime() override
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition
group-core-actions.dox:1
QMultiMap
QString
QString::arg
QString arg(Args &&... args) const const
QString::isEmpty
bool isEmpty() const const
QTime
QTime::isValid
bool isValid(int h, int m, int s, int ms)
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
validatortime.cpp
Generated by
1.9.8