cutelyst 3.9.1
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-2022 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
23
25{
27
28 Q_D(const ValidatorDate);
29
30 const QString v = value(params);
31
32 if (!v.isEmpty()) {
33 const QDate date = d->extractDate(c, v, d->inputFormat);
34
35 if (!date.isValid()) {
36 result.errorMessage = validationError(c);
37 qCDebug(C_VALIDATOR,
38 "ValidatorDate: Validation failed for value \"%s\" in field %s in %s::%s: not "
39 "a valid date.",
40 qPrintable(v),
41 qPrintable(field()),
42 qPrintable(c->controllerName()),
43 qPrintable(c->actionName()));
44 } else {
45 result.value.setValue(date);
46 }
47 } else {
48 defaultValue(c, &result, "ValidatorDate");
49 }
50
51 return result;
52}
53
55{
56 QString error;
57
58 Q_D(const ValidatorDate);
59 Q_UNUSED(errorData)
60
61 const QString _label = label(c);
62
63 if (_label.isEmpty()) {
64
65 if (d->inputFormat) {
66 //: %1 will be replaced by the date format
67 error = c->translate("Cutelyst::ValidatorDate",
68 "Not a valid date according to the following date format: %1")
69 .arg(c->translate(d->translationContext.data(), d->inputFormat));
70 } else {
71 error = c->translate("Cutelyst::ValidatorDate", "Not a valid date.");
72 }
73
74 } else {
75
76 if (d->inputFormat) {
77 //: %1 will be replaced by the field label, %2 will be replaced by the date format
78 error = c->translate("Cutelyst::ValidatorDate",
79 "The value in the “%1” field can not be parsed as date according "
80 "to the following scheme: %2")
81 .arg(_label, c->translate(d->translationContext.data(), d->inputFormat));
82 } else {
83 //: %1 will be replaced by the field label
84 error = c->translate("Cutelyst::ValidatorDate",
85 "The value in the “%1” field can not be parsed as date.")
86 .arg(_label);
87 }
88 }
89
90 return error;
91}
The Cutelyst Context.
Definition context.h:39
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition context.cpp:490
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.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString field() const
Returns the name of the field to validate.
void defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
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.
The Cutelyst namespace holds all public Cutelyst API.
Definition Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
bool isValid() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
void setValue(const T &value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.