cutelyst 4.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-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
10using namespace Cutelyst;
11
12ValidatorDate::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
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
47QString ValidatorDate::genericValidationError(Context *c, const QVariant &errorData) const
48{
49 QString error;
50
51 Q_D(const ValidatorDate);
52 Q_UNUSED(errorData)
53
54 const QString _label = label(c);
55
56 if (_label.isEmpty()) {
57
58 if (d->inputFormat) {
59 //: %1 will be replaced by the date format
60 error = c->translate("Cutelyst::ValidatorDate",
61 "Not a valid date according to the following date format: %1")
62 .arg(c->translate(d->translationContext.data(), d->inputFormat));
63 } else {
64 error = c->translate("Cutelyst::ValidatorDate", "Not a valid date.");
65 }
66
67 } else {
68
69 if (d->inputFormat) {
70 //: %1 will be replaced by the field label, %2 will be replaced by the date format
71 error = c->translate("Cutelyst::ValidatorDate",
72 "The value in the “%1” field can not be parsed as date according "
73 "to the following scheme: %2")
74 .arg(_label, c->translate(d->translationContext.data(), d->inputFormat));
75 } else {
76 //: %1 will be replaced by the field label
77 error = c->translate("Cutelyst::ValidatorDate",
78 "The value in the “%1” field can not be parsed as date.")
79 .arg(_label);
80 }
81 }
82
83 return error;
84}
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 date.
Definition: validatordate.h:41
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
~ValidatorDate() override
Deconstructs the date validator.
ValidatorDate(const QString &field, const char *inputFormat=nullptr, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new date validator.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
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