cutelyst  4.6.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-2023 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 
25 {
26  ValidatorReturnType result;
27 
28  Q_D(const ValidatorDateTime);
29 
30  const QString v = value(params);
31 
32  if (!v.isEmpty()) {
33  const QTimeZone tz = d->extractTimeZone(c, params, d->timeZone);
34  const QDateTime dt = d->extractDateTime(c, v, d->inputFormat, tz);
35 
36  if (!dt.isValid()) {
37  result.errorMessage = validationError(c);
38  qCDebug(C_VALIDATOR).noquote().nospace()
39  << debugString(c) << " \"" << v << "\" is not a valid datetime";
40  } else {
41  result.value.setValue(dt);
42  }
43 
44  } else {
45  defaultValue(c, &result);
46  }
47 
48  return result;
49 }
50 
52 {
53  Q_D(const ValidatorDateTime);
54  Q_UNUSED(errorData)
55 
56  const QString _label = label(c);
57 
58  if (d->inputFormat) {
59  const QString inputFormatTranslated =
60  d->translationContext ? c->translate(d->translationContext, d->inputFormat)
61  : c->qtTrId(d->inputFormat);
62  if (_label.isEmpty()) {
63  //: %1 will be replaced by the required date and time format
64  //% "Not a valid date and time according to the following format: %1"
65  return c->qtTrId("cutelyst-valdatetime-genvalerr-format").arg(inputFormatTranslated);
66  } else {
67  //: %1 will be replaced by the field label, %2 will be replaced by
68  //: the required datetime format
69  //% "The value in the “%1” field can not be parsed as date and time "
70  //% "according to the following date and time format: %2"
71  return c->qtTrId("cutelyst-valdatetime-genvalerr-format-label")
72  .arg(_label, inputFormatTranslated);
73  }
74  } else {
75  if (_label.isEmpty()) {
76  //% "Not a valid date and time."
77  return c->qtTrId("cutelyst-valdatetime-genvalerr");
78  } else {
79  //: %1 will be replaced by the field label
80  //% "The value in the “%1” field can not be parsed as date and time."
81  return c->qtTrId("cutelyst-valdatetime-genvalerr-label").arg(_label);
82  }
83  }
84 }
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:484
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:656
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())
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
The Cutelyst namespace holds all public Cutelyst API.
bool isValid() const const
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.
Definition: validatorrule.h:49