cutelyst 5.0.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-2025 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatordate_p.h"
7
8#include <QDate>
9
10using namespace Cutelyst;
11
13 const char *inputFormat,
14 const Cutelyst::ValidatorMessages &messages,
15 const QString &defValKey)
16 : ValidatorRule(*new ValidatorDatePrivate(field, inputFormat, messages, defValKey))
17{
18}
19
21
23{
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
48{
49 cb(validate(c, params));
50}
51
53{
54 Q_D(const ValidatorDate);
55 Q_UNUSED(errorData)
56
57 const QString _label = label(c);
58
59 if (d->inputFormat) {
60 const QString inputFormatTranslated =
61 d->translationContext ? c->translate(d->translationContext, d->inputFormat)
62 : c->qtTrId(d->inputFormat);
63 if (_label.isEmpty()) {
64 //: %1 will be replaced by the required date format
65 //% "Not a valid date according to the following format: %1"
66 return c->qtTrId("cutelyst-valdate-genvalerr-format").arg(inputFormatTranslated);
67 } else {
68 //: %1 will be replaced by the field label, %2 will be replaced
69 //: by the required date format
70 //% "The value in the “%1” field can not be parsed as date according "
71 //% "to the following format: %2"
72 return c->qtTrId("cutelyst-valdate-genvalerr-format-label")
73 .arg(_label, inputFormatTranslated);
74 }
75 } else {
76 if (_label.isEmpty()) {
77 //% "Not a valid date."
78 return c->qtTrId("cutelyst-valdate-genvalerr");
79 } else {
80 //: %1 will be replaced by the field label
81 //% "The value in the “%1” field can not be parsed as date."
82 return c->qtTrId("cutelyst-valdate-genvalerr-label").arg(_label);
83 }
84 }
85}
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 date.
ValidatorDate(const QString &field, const char *inputFormat=nullptr, const ValidatorMessages &messages={}, const QString &defValKey={})
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
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(int year, int month, int day)
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.