cutelyst  4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorafter.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorafter_p.h"
7 
8 #include <QLocale>
9 #include <QTimeZone>
10 
11 using namespace Cutelyst;
12 
14  const QVariant &comparison,
15  const QString &timeZone,
16  const char *inputFormat,
17  const Cutelyst::ValidatorMessages &messages,
18  const QString &defValKey)
19  : ValidatorRule(
20  *new ValidatorAfterPrivate(field, comparison, timeZone, inputFormat, messages, defValKey))
21 {
22 }
23 
25 
27 {
28  ValidatorReturnType result;
29 
30  Q_D(const ValidatorAfter);
31 
32  const QString v = value(params);
33 
34  if (!v.isEmpty()) {
35 
36  const QTimeZone tz = d->extractTimeZone(c, params, d->timeZone);
37 
38  const QVariant _comp =
39  (d->comparison.userType() == QMetaType::QString)
40  ? d->extractOtherDateTime(c, params, d->comparison.toString(), tz, d->inputFormat)
41  : d->comparison;
42 
43  if (_comp.userType() == QMetaType::QDate) {
44 
45  const QDate odate = _comp.toDate();
46  if (Q_UNLIKELY(!odate.isValid())) {
47  qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison date";
49  } else {
50  const QDate date = d->extractDate(c, v, d->inputFormat);
51  if (Q_UNLIKELY(!date.isValid())) {
52  qCWarning(C_VALIDATOR).noquote().nospace()
53  << debugString(c) << " Can not parse input date \"" << v << "\"";
54  result.errorMessage = parsingError(c, odate);
55  } else {
56  if (Q_UNLIKELY(date <= odate)) {
57  qCDebug(C_VALIDATOR).noquote()
58  << debugString(c) << "Input" << date << "is not after" << odate;
59  result.errorMessage = validationError(c, odate);
60  } else {
61  result.value.setValue(date);
62  }
63  }
64  }
65 
66  } else if (_comp.userType() == QMetaType::QDateTime) {
67 
68  const QDateTime odatetime = _comp.toDateTime();
69  if (Q_UNLIKELY(!odatetime.isValid())) {
70  qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison datetime";
72  } else {
73  const QDateTime datetime = d->extractDateTime(c, v, d->inputFormat, tz);
74  if (Q_UNLIKELY(!datetime.isValid())) {
75  qCWarning(C_VALIDATOR).noquote().nospace()
76  << debugString(c) << " Can not parse input datetime \"" << v << "\"";
77  result.errorMessage = parsingError(c, odatetime);
78  } else {
79  if (Q_UNLIKELY(datetime <= odatetime)) {
80  qCDebug(C_VALIDATOR).noquote()
81  << debugString(c) << "Input" << datetime << "is not after" << odatetime;
82  result.errorMessage = validationError(c, odatetime);
83  } else {
84  result.value.setValue(datetime);
85  }
86  }
87  }
88 
89  } else if (_comp.userType() == QMetaType::QTime) {
90 
91  const QTime otime = _comp.toTime();
92  if (Q_UNLIKELY(!otime.isValid())) {
93  qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison time";
95  } else {
96  const QTime time = d->extractTime(c, v, d->inputFormat);
97  if (Q_UNLIKELY(!time.isValid())) {
98  qCWarning(C_VALIDATOR).noquote().nospace()
99  << debugString(c) << " Can not parse input time \"" << v << "\"";
100  result.errorMessage = parsingError(c, otime);
101  } else {
102  if (Q_UNLIKELY(time <= otime)) {
103  qCDebug(C_VALIDATOR).noquote()
104  << debugString(c) << "Input" << time << "is not after" << otime;
105  result.errorMessage = validationError(c, otime);
106  } else {
107  result.value.setValue(time);
108  }
109  }
110  }
111 
112  } else {
113  qCWarning(C_VALIDATOR).noquote()
114  << debugString(c) << "Invalid comparison data:" << d->comparison;
115  result.errorMessage = validationDataError(c);
116  }
117  } else {
118  defaultValue(c, &result);
119  }
120 
121  return result;
122 }
123 
125  const QVariant &errorData) const
126 {
127  const QString _label = label(c);
128  if (_label.isEmpty()) {
129 
130  switch (errorData.userType()) {
131  case QMetaType::QDate:
132  //: %1 will be replaced by the comparison date in short format
133  //% "Has to be after %1."
134  return c->qtTrId("cutelyst-valafter-genvalerr-date")
135  .arg(c->locale().toString(errorData.toDate(), QLocale::ShortFormat));
137  //: %1 will be replaced by the comparison datetime in short format
138  //% "Has to be after %1."
139  return c->qtTrId("cutelyst-valafter-genvalerr-dt")
140  .arg(c->locale().toString(errorData.toDateTime(), QLocale::ShortFormat));
141  case QMetaType::QTime:
142  //: %1 will be replaced by the comparison time in short format
143  //% "Has to be after %1."
144  return c->qtTrId("cutelyst-valafter-genvalerr-time")
145  .arg(c->locale().toString(errorData.toTime(), QLocale::ShortFormat));
146  default:
147  return validationDataError(c);
148  }
149 
150  } else {
151 
152  switch (errorData.userType()) {
153  case QMetaType::QDate:
154  //: %1 will be rplaced by the field label, %2 by the comparison date in short format
155  //% "The date in the “%1” field must be after %2."
156  return c->qtTrId("cutelyst-valafter-genvalerr-date-label")
157  .arg(_label, c->locale().toString(errorData.toDate(), QLocale::ShortFormat));
159  //: %1 will be replaced by the field label, %2 by the comparison datetime in short
160  //: format
161  //% "The date and time in the “%1” field must be after %2."
162  return c->qtTrId("cutelyst-valafter-genvalerr-dt-label")
163  .arg(_label, c->locale().toString(errorData.toDateTime(), QLocale::ShortFormat));
164  case QMetaType::QTime:
165  //: %1 will be replaced by the field label, %2 by the comparison time in short format
166  //% "The time in the “%1” field must be after %2."
167  return c->qtTrId("cutelyst-valafter-genvalerr-time-label")
168  .arg(_label, c->locale().toString(errorData.toTime(), QLocale::ShortFormat));
169  default:
170  return validationDataError(c);
171  }
172  }
173 }
174 
176 {
177  Q_UNUSED(errorData)
178  const QString _label = label(c);
179  if (_label.isEmpty()) {
180  //% "The comparison value is not a valid date and/or time, or can not be found."
181  return c->qtTrId("cutelyst-validator-genvaldataerr-dt");
182  } else {
183  //: %1 will be replaced by the field label
184  //% "The comparison value for the “%1” field is not a valid date and/or time, or "
185  //% "can not be found."
186  return c->qtTrId("cutelyst-validator-genvaldataerr-dt-label").arg(_label);
187  }
188 }
189 
191 {
192  Q_D(const ValidatorAfter);
193 
194  const QString _label = label(c);
195  if (d->inputFormat) {
196  const QString _inputFormatTranslated =
197  d->translationContext ? c->translate(d->translationContext, d->inputFormat)
198  : c->qtTrId(d->inputFormat);
199  if (_label.isEmpty()) {
200  //: %1 will be replaced by the required input format
201  //% "Could not be parsed according to the following date and/or time format: %1"
202  return c->qtTrId("cutelyst-validator-genparseerr-dt-format")
203  .arg(_inputFormatTranslated);
204  } else {
205  //: %1 will be replaced by the field label, %2 by the required input format
206  //% "The value of the “%1” field could not be parsed according to the "
207  //% "following date and/or time format: %2"
208  return c->qtTrId("cutelyst-validator-genparseerr-dt-format-label")
209  .arg(_label, _inputFormatTranslated);
210  }
211  } else {
212 
213  if (_label.isEmpty()) {
214  switch (errorData.userType()) {
216  //% "Could not be parsed as date and time."
217  return c->qtTrId("cutelyst-validator-genparseerr-dt");
218  case QMetaType::QTime:
219  //% "Could not be parsed as time."
220  return c->qtTrId("cutelyst-validator-genparseerr-time");
221  case QMetaType::QDate:
222  //% "Could not be parsed as date."
223  return c->qtTrId("cutelyst-validator-genparseerr-date");
224  default:
225  return validationDataError(c);
226  }
227  } else {
228  switch (errorData.userType()) {
230  //: %1 will be replaced by the field label
231  //% "The value in the “%1” field could not be parsed as date and time."
232  return c->qtTrId("cutelyst-vaidator-genparseerr-dt-label").arg(_label);
233  case QMetaType::QTime:
234  //: %1 will be replaced by the field label
235  //% "The value in the “%1” field could not be parsed as time."
236  return c->qtTrId("cutelyst-validator-genparseerr-time-label").arg(_label);
237  case QMetaType::QDate:
238  //: %1 will be replaced by the field label
239  //% "The value in the “%1” field could not be parsed as date."
240  return c->qtTrId("cutelyst-validator-genparseerr-date-label").arg(_label);
241  default:
242  return validationDataError(c);
243  }
244  }
245  }
246 }
The Cutelyst Context.
Definition: context.h:42
QLocale locale() const noexcept
Definition: context.cpp:460
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 a date, time or datetime is after a comparison value.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
ValidatorAfter(const QString &field, const QVariant &comparison, const QString &timeZone=QString(), const char *inputFormat=nullptr, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const override
QString genericParsingError(Context *c, const QVariant &errorData=QVariant()) const override
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
QString validationDataError(Context *c, const QVariant &errorData={}) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString parsingError(Context *c, const QVariant &errorData={}) const
QString debugString(Context *c) const
The Cutelyst namespace holds all public Cutelyst API.
bool isValid(int year, int month, int day)
bool isValid() const const
QString toString(QDate date, QLocale::FormatType format) const const
QString arg(Args &&... args) const const
bool isEmpty() const const
bool isValid(int h, int m, int s, int ms)
void setValue(QVariant &&value)
QDate toDate() const const
QDateTime toDateTime() const const
QTime toTime() const const
int userType() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49