cutelyst 4.0.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
10using namespace Cutelyst;
11
13 const QString &timeZone,
14 const char *inputFormat,
15 const ValidatorMessages &messages,
16 const QString &defValKey)
18 *new ValidatorDateTimePrivate(field, timeZone, inputFormat, messages, defValKey))
19{
20}
21
23
25{
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
51QString ValidatorDateTime::genericValidationError(Context *c, const QVariant &errorData) const
52{
53 QString error;
54
55 Q_D(const ValidatorDateTime);
56 Q_UNUSED(errorData)
57
58 const QString _label = label(c);
59
60 if (_label.isEmpty()) {
61
62 if (d->inputFormat) {
63 //: %1 will be replaced by the datetime format
64 error = c->translate("Cutelyst::ValidatorDateTime",
65 "Not a valid date and time according to the following format: %1")
66 .arg(c->translate(d->translationContext.data(), d->inputFormat));
67 } else {
68 error = c->translate("Cutelyst::ValidatorDateTime", "Not a valid date and time.");
69 }
70
71 } else {
72
73 if (d->inputFormat) {
74 //: %1 will be replaced by the field label, %2 will be replaced by the datetime format
75 error = c->translate("Cutelyst::ValidatorDateTime",
76 "The value in the “%1” field can not be parsed as date and time "
77 "according to the following date and time format: %2")
78 .arg(_label, c->translate(d->translationContext.data(), d->inputFormat));
79 } else {
80 //: %1 will be replaced by the field label
81 error = c->translate("Cutelyst::ValidatorDateTime",
82 "The value in the “%1” field can not be parsed as date and time.")
83 .arg(_label);
84 }
85 }
86
87 return error;
88}
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
Checks if the input data is a valid datetime.
ValidatorDateTime(const QString &field, const QString &timeZone, const char *inputFormat=nullptr, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new datetime validator.
~ValidatorDateTime() override
Deconstructs the datetime validator.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
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