cutelyst  3.7.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-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatordatetime_p.h"
7 
8 #include <QDateTime>
9 
10 using namespace Cutelyst;
11 
12 ValidatorDateTime::ValidatorDateTime(const QString &field, const QString &timeZone, const char *inputFormat, const ValidatorMessages &messages, const QString &defValKey) :
13  ValidatorRule(*new ValidatorDateTimePrivate(field, timeZone, inputFormat, messages, defValKey))
14 {
15 }
16 
18 {
19 }
20 
22 {
23  ValidatorReturnType result;
24 
25  Q_D(const ValidatorDateTime);
26 
27  const QString v = value(params);
28 
29  if (!v.isEmpty()) {
30  const QTimeZone tz = d->extractTimeZone(c, params, d->timeZone);
31  const QDateTime dt = d->extractDateTime(c, v, d->inputFormat, tz);
32 
33  if (!dt.isValid()) {
34  result.errorMessage = validationError(c);
35  qCDebug(C_VALIDATOR, "ValidatorDateTime: Validation failed for value \"%s\" in field %s in %s::%s: not a valid date and time.", qPrintable(v), qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
36  } else {
37  result.value.setValue(dt);
38  }
39 
40  } else {
41  defaultValue(c, &result, "ValidatorDateTime");
42  }
43 
44  return result;
45 }
46 
47 QString ValidatorDateTime::genericValidationError(Context *c, const QVariant &errorData) const
48 {
49  QString error;
50 
51  Q_D(const ValidatorDateTime);
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 datetime format
60  error = c->translate("Cutelyst::ValidatorDateTime", "Not a valid date and time according to the following format: %1").arg(c->translate(d->translationContext.data(), d->inputFormat));
61  } else {
62  error = c->translate("Cutelyst::ValidatorDateTime", "Not a valid date and time.");
63  }
64 
65  } else {
66 
67  if (d->inputFormat) {
68  //: %1 will be replaced by the field label, %2 will be replaced by the datetime format
69  error = c->translate("Cutelyst::ValidatorDateTime", "The value in the “%1” field can not be parsed as date and time according to the following date and time format: %2").arg(_label, c->translate(d->translationContext.data(), d->inputFormat));
70  } else {
71  //: %1 will be replaced by the field label
72  error = c->translate("Cutelyst::ValidatorDateTime", "The value in the “%1” field can not be parsed as date and time.").arg(_label);
73  }
74  }
75 
76  return error;
77 }
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: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.
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 ...
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
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49