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