cutelyst 5.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-2025 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 = ValidatorDateTimePrivate::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
52{
53 cb(validate(c, params));
54}
55
57{
58 Q_D(const ValidatorDateTime);
59 Q_UNUSED(errorData)
60
61 const QString _label = label(c);
62
63 if (d->inputFormat) {
64 const QString inputFormatTranslated =
65 d->translationContext ? c->translate(d->translationContext, d->inputFormat)
66 : c->qtTrId(d->inputFormat);
67 if (_label.isEmpty()) {
68 //: %1 will be replaced by the required date and time format
69 //% "Not a valid date and time according to the following format: %1"
70 return c->qtTrId("cutelyst-valdatetime-genvalerr-format").arg(inputFormatTranslated);
71 } else {
72 //: %1 will be replaced by the field label, %2 will be replaced by
73 //: the required datetime format
74 //% "The value in the “%1” field can not be parsed as date and time "
75 //% "according to the following date and time format: %2"
76 return c->qtTrId("cutelyst-valdatetime-genvalerr-format-label")
77 .arg(_label, inputFormatTranslated);
78 }
79 } else {
80 if (_label.isEmpty()) {
81 //% "Not a valid date and time."
82 return c->qtTrId("cutelyst-valdatetime-genvalerr");
83 } else {
84 //: %1 will be replaced by the field label
85 //% "The value in the “%1” field can not be parsed as date and time."
86 return c->qtTrId("cutelyst-valdatetime-genvalerr-label").arg(_label);
87 }
88 }
89}
The Cutelyst Context.
Definition context.h:42
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition context.cpp:485
QString qtTrId(const char *id, int n=-1) const
Definition context.h:657
Checks if the input data is a valid datetime.
ValidatorDateTime(const QString &field, const QString &timeZone, const char *inputFormat=nullptr, const ValidatorMessages &messages={}, const QString &defValKey={})
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(const Context *c) const
QString debugString(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
The Cutelyst namespace holds all public Cutelyst API.
bool isValid() const const
QString arg(Args &&... args) const const
bool isEmpty() const const
void setValue(QVariant &&value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.