cutelyst 4.0.0
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
11using namespace Cutelyst;
12
13ValidatorAfter::ValidatorAfter(const QString &field,
14 const QVariant &comparison,
15 const QString &timeZone,
16 const char *inputFormat,
17 const Cutelyst::ValidatorMessages &messages,
18 const QString &defValKey)
20 *new ValidatorAfterPrivate(field, comparison, timeZone, inputFormat, messages, defValKey))
21{
22}
23
25
27{
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;
116 }
117 } else {
118 defaultValue(c, &result);
119 }
120
121 return result;
122}
123
125 const QVariant &errorData) const
126{
127 QString error;
128
129 const QString _label = label(c);
130 if (_label.isEmpty()) {
131
132 switch (errorData.userType()) {
133 case QMetaType::QDate:
134 error =
135 c->translate("Cutelyst::ValidatorAfter", "Has to be after %1.")
136 .arg(errorData.toDate().toString(c->locale().dateFormat(QLocale::ShortFormat)));
137 break;
138 case QMetaType::QDateTime:
139 error = c->translate("Cutelyst::ValidatorAfter", "Has to be after %1.")
140 .arg(errorData.toDateTime().toString(
141 c->locale().dateTimeFormat(QLocale::ShortFormat)));
142 break;
143 case QMetaType::QTime:
144 error =
145 c->translate("Cutelyst::ValidatorAfter", "Has to be after %1.")
146 .arg(errorData.toTime().toString(c->locale().timeFormat(QLocale::ShortFormat)));
147 break;
148 default:
149 error = validationDataError(c);
150 break;
151 }
152
153 } else {
154
155 switch (errorData.userType()) {
156 case QMetaType::QDate:
157 error =
158 c->translate("Cutelyst::ValidatorAfter",
159 "The date in the “%1” field must be after %2.")
160 .arg(_label,
161 errorData.toDate().toString(c->locale().dateFormat(QLocale::ShortFormat)));
162 break;
163 case QMetaType::QDateTime:
164 error = c->translate("Cutelyst::ValidatorAfter",
165 "The date and time in the “%1” field must be after %2.")
166 .arg(_label,
167 errorData.toDateTime().toString(
168 c->locale().dateTimeFormat(QLocale::ShortFormat)));
169 break;
170 case QMetaType::QTime:
171 error =
172 c->translate("Cutelyst::ValidatorAfter",
173 "The time in the “%1” field must be after %2.")
174 .arg(_label,
175 errorData.toTime().toString(c->locale().timeFormat(QLocale::ShortFormat)));
176 break;
177 default:
178 error = validationDataError(c);
179 break;
180 }
181 }
182
183 return error;
184}
185
186QString ValidatorAfter::genericValidationDataError(Context *c, const QVariant &errorData) const
187{
188 QString error;
189
190 Q_UNUSED(errorData)
191 error =
192 c->translate("Cutelyst::ValidatorAfter",
193 "The comparison value is not a valid date and/or time, or cannot be found.");
194
195 return error;
196}
197
198QString ValidatorAfter::genericParsingError(Context *c, const QVariant &errorData) const
199{
200 QString error;
201
202 Q_D(const ValidatorAfter);
203
204 const QString _label = label(c);
205 if (d->inputFormat) {
206 if (_label.isEmpty()) {
207 error =
208 c->translate(
209 "Cutelyst::ValidatorAfter",
210 "Could not be parsed according to the following date and/or time format: %1")
211 .arg(c->translate(d->translationContext.data(), d->inputFormat));
212 } else {
213 error = c->translate("Cutelyst::ValidatorAfter",
214 "The value of the “%1” field could not be parsed according to the "
215 "following date and/or time format: %2")
216 .arg(_label, c->translate(d->translationContext.data(), d->inputFormat));
217 }
218 } else {
219
220 if (_label.isEmpty()) {
221 switch (errorData.userType()) {
222 case QMetaType::QDateTime:
223 error = c->translate("Cutelyst::ValidatorAfter",
224 "Could not be parsed as date and time.");
225 break;
226 case QMetaType::QTime:
227 error = c->translate("Cutelyst::ValidatorAfter", "Could not be parsed as time.");
228 break;
229 case QMetaType::QDate:
230 error = c->translate("Cutelyst::ValidatorAfter", "Could not be parsed as date.");
231 break;
232 default:
233 error = validationDataError(c);
234 break;
235 }
236 } else {
237 switch (errorData.userType()) {
238 case QMetaType::QDateTime:
239 //: %1 will be replaced by the field label
240 error = c->translate(
241 "Cutelyst::ValidatorAfter",
242 "The value in the “%1” field could not be parsed as date and time.")
243 .arg(_label);
244 break;
245 case QMetaType::QTime:
246 //: %1 will be replaced by the field label
247 error = c->translate("Cutelyst::ValidatorAfter",
248 "The value in the “%1” field could not be parsed as time.")
249 .arg(_label);
250 break;
251 case QMetaType::QDate:
252 //: %1 will be replaced by the field label
253 error = c->translate("Cutelyst::ValidatorAfter",
254 "The value in the “%1” field could not be parsed as date.")
255 .arg(_label);
256 break;
257 default:
258 error = validationDataError(c);
259 break;
260 }
261 }
262 }
263
264 return error;
265}
The Cutelyst Context.
Definition: context.h:38
QLocale locale() const noexcept
Definition: context.cpp:453
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
Checks if a date, time or datetime is after a comparison value.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
ValidatorAfter(const QString &field, const QVariant &comparison, const QString &timeZone=QString(), const char *inputFormat=nullptr, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new after validator.
~ValidatorAfter() override
Deconstructs the after validator.
QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if comparison data was invalid.
QString genericParsingError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if the input value could not be parsed.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occurred while parsing input.
void defaultValue(Context *c, ValidatorReturnType *result) 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 validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
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