cutelyst  3.7.0
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 #include <QDate>
8 
9 using namespace Cutelyst;
10 
11 ValidatorDate::ValidatorDate(const QString &field, const char *inputFormat, const Cutelyst::ValidatorMessages &messages, const QString &defValKey) :
12  ValidatorRule(*new ValidatorDatePrivate(field, inputFormat, messages, defValKey))
13 {
14 }
15 
17 {
18 }
19 
20 
22 {
23  ValidatorReturnType result;
24 
25  Q_D(const ValidatorDate);
26 
27  const QString v = value(params);
28 
29  if (!v.isEmpty()) {
30  const QDate date = d->extractDate(c, v, d->inputFormat);
31 
32  if (!date.isValid()) {
33  result.errorMessage = validationError(c);
34  qCDebug(C_VALIDATOR, "ValidatorDate: Validation failed for value \"%s\" in field %s in %s::%s: not a valid date.", qPrintable(v), qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
35  } else {
36  result.value.setValue(date);
37  }
38  } else {
39  defaultValue(c, &result, "ValidatorDate");
40  }
41 
42  return result;
43 }
44 
45 QString ValidatorDate::genericValidationError(Context *c, const QVariant &errorData) const
46 {
47  QString error;
48 
49  Q_D(const ValidatorDate);
50  Q_UNUSED(errorData)
51 
52  const QString _label = label(c);
53 
54  if (_label.isEmpty()) {
55 
56  if (d->inputFormat) {
57  //: %1 will be replaced by the date format
58  error = c->translate("Cutelyst::ValidatorDate", "Not a valid date according to the following date format: %1").arg(c->translate(d->translationContext.data(), d->inputFormat));
59  } else {
60  error = c->translate("Cutelyst::ValidatorDate", "Not a valid date.");
61  }
62 
63  } else {
64 
65  if (d->inputFormat) {
66  //: %1 will be replaced by the field label, %2 will be replaced by the date format
67  error = c->translate("Cutelyst::ValidatorDate", "The value in the “%1” field can not be parsed as date according to the following scheme: %2").arg(_label, c->translate(d->translationContext.data(), d->inputFormat));
68  } else {
69  //: %1 will be replaced by the field label
70  error = c->translate("Cutelyst::ValidatorDate", "The value in the “%1” field can not be parsed as date.").arg(_label);
71  }
72  }
73 
74  return error;
75 }
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 date.
Definition: validatordate.h:38
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.
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