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 
10 using 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 
21 {
22 }
23 
25 {
26  ValidatorReturnType result;
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 }
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.
ValidatorDate(const QString &field, const char *inputFormat=nullptr, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new date validator.
The Cutelyst Context.
Definition: context.h:38
bool isEmpty() const const
Checks if the input data is a valid date.
Definition: validatordate.h:40
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:490
bool isValid() const const
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.
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
~ValidatorDate() override
Deconstructs the date validator.
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 ...