cutelyst  3.9.1
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 
13  const QString &timeZone,
14  const char *inputFormat,
15  const ValidatorMessages &messages,
16  const QString &defValKey)
17  : ValidatorRule(
18  *new ValidatorDateTimePrivate(field, timeZone, inputFormat, messages, defValKey))
19 {
20 }
21 
23 {
24 }
25 
27 {
28  ValidatorReturnType result;
29 
30  Q_D(const ValidatorDateTime);
31 
32  const QString v = value(params);
33 
34  if (!v.isEmpty()) {
35  const QTimeZone tz = d->extractTimeZone(c, params, d->timeZone);
36  const QDateTime dt = d->extractDateTime(c, v, d->inputFormat, tz);
37 
38  if (!dt.isValid()) {
39  result.errorMessage = validationError(c);
40  qCDebug(C_VALIDATOR,
41  "ValidatorDateTime: Validation failed for value \"%s\" in field %s in %s::%s: "
42  "not a valid date and time.",
43  qPrintable(v),
44  qPrintable(field()),
45  qPrintable(c->controllerName()),
46  qPrintable(c->actionName()));
47  } else {
48  result.value.setValue(dt);
49  }
50 
51  } else {
52  defaultValue(c, &result, "ValidatorDateTime");
53  }
54 
55  return result;
56 }
57 
59 {
60  QString error;
61 
62  Q_D(const ValidatorDateTime);
63  Q_UNUSED(errorData)
64 
65  const QString _label = label(c);
66 
67  if (_label.isEmpty()) {
68 
69  if (d->inputFormat) {
70  //: %1 will be replaced by the datetime format
71  error = c->translate("Cutelyst::ValidatorDateTime",
72  "Not a valid date and time according to the following format: %1")
73  .arg(c->translate(d->translationContext.data(), d->inputFormat));
74  } else {
75  error = c->translate("Cutelyst::ValidatorDateTime", "Not a valid date and time.");
76  }
77 
78  } else {
79 
80  if (d->inputFormat) {
81  //: %1 will be replaced by the field label, %2 will be replaced by the datetime format
82  error = c->translate("Cutelyst::ValidatorDateTime",
83  "The value in the “%1” field can not be parsed as date and time "
84  "according to the following date and time format: %2")
85  .arg(_label, c->translate(d->translationContext.data(), d->inputFormat));
86  } else {
87  //: %1 will be replaced by the field label
88  error = c->translate("Cutelyst::ValidatorDateTime",
89  "The value in the “%1” field can not be parsed as date and time.")
90  .arg(_label);
91  }
92  }
93 
94  return error;
95 }
Checks if the input data is a valid datetime.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Stores custom error messages and the input field label.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
The Cutelyst Context.
Definition: context.h:38
bool isEmpty() const const
ValidatorDateTime(const QString &field, const QString &timeZone, const char *inputFormat=nullptr, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new datetime validator.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:490
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
~ValidatorDateTime() override
Deconstructs the datetime validator.
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
bool isValid() const const
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
QString arg(Args &&... args) const const
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
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 ...
void setValue(QVariant &&value)