cutelyst 4.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorbefore.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorbefore_p.h"
7
8#include <QtCore/QLoggingCategory>
9
10using namespace Cutelyst;
11
12Q_LOGGING_CATEGORY(C_VALIDATORBEFORE, "cutelyst.utils.validator.before", QtWarningMsg)
13
14ValidatorBefore::ValidatorBefore(const QString &field,
15 const QVariant &comparison,
16 const QString &timeZone,
17 const char *inputFormat,
18 const ValidatorMessages &messages,
19 const QString &defValKey)
20 : ValidatorRule(*new ValidatorBeforePrivate(field,
21 comparison,
22 timeZone,
23 inputFormat,
24 messages,
25 defValKey))
26{
27}
28
30
32{
34
35 Q_D(const ValidatorBefore);
36
37 const QString v = value(params);
38
39 if (!v.isEmpty()) {
40
41 const QTimeZone tz = d->extractTimeZone(c, params, d->timeZone);
42
43 const QVariant _comp =
44 (d->comparison.userType() == QMetaType::QString)
45 ? d->extractOtherDateTime(c, params, d->comparison.toString(), tz, d->inputFormat)
46 : d->comparison;
47
48 if (_comp.userType() == QMetaType::QDate) {
49
50 const QDate odate = _comp.toDate();
51 if (Q_UNLIKELY(!odate.isValid())) {
52 qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison date";
54 } else {
55 const QDate date = d->extractDate(c, v, d->inputFormat);
56 if (Q_UNLIKELY(!date.isValid())) {
57 qCWarning(C_VALIDATOR).noquote().nospace()
58 << debugString(c) << " Can not parse input date \"" << v << "\"";
59 result.errorMessage = parsingError(c, odate);
60 } else {
61 if (Q_UNLIKELY(date >= odate)) {
62 qCDebug(C_VALIDATOR).noquote()
63 << debugString(c) << "Input" << date << "is not before" << odate;
64 result.errorMessage = validationError(c, odate);
65 } else {
66 result.value.setValue(date);
67 }
68 }
69 }
70
71 } else if (_comp.userType() == QMetaType::QDateTime) {
72
73 const QDateTime odatetime = _comp.toDateTime();
74 if (Q_UNLIKELY(!odatetime.isValid())) {
75 qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison datetime";
77 } else {
78 const QDateTime datetime = d->extractDateTime(c, v, d->inputFormat, tz);
79 if (Q_UNLIKELY(!datetime.isValid())) {
80 qCWarning(C_VALIDATOR).noquote().nospace()
81 << debugString(c) << " Can not parse input datetime \"" << v << "\"";
82 result.errorMessage = parsingError(c, odatetime);
83 } else {
84 if (Q_UNLIKELY(datetime >= odatetime)) {
85 qCDebug(C_VALIDATOR).noquote() << debugString(c) << "Input" << datetime
86 << "is not before" << odatetime;
87 result.errorMessage = validationError(c, odatetime);
88 } else {
89 result.value.setValue(datetime);
90 }
91 }
92 }
93
94 } else if (_comp.userType() == QMetaType::QTime) {
95
96 const QTime otime = _comp.toTime();
97 if (Q_UNLIKELY(!otime.isValid())) {
98 qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison time";
100 } else {
101 const QTime time = d->extractTime(c, v, d->inputFormat);
102 if (Q_UNLIKELY(!time.isValid())) {
103 qCWarning(C_VALIDATOR).noquote().nospace()
104 << debugString(c) << " Can not parse input time \"" << v << "\"";
105 result.errorMessage = parsingError(c, otime);
106 } else {
107 if (Q_UNLIKELY(time >= otime)) {
108 qCDebug(C_VALIDATOR).noquote()
109 << debugString(c) << "Input" << time << "is not before" << otime;
110 result.errorMessage = validationError(c, otime);
111 } else {
112 result.value.setValue(time);
113 }
114 }
115 }
116
117 } else {
118 qCWarning(C_VALIDATOR).noquote()
119 << debugString(c) << "Invalid comparison data:" << d->comparison;
121 }
122 } else {
123 defaultValue(c, &result);
124 }
125
126 return result;
127}
128
130 const QVariant &errorData) const
131{
132 QString error;
133
134 const QString _label = label(c);
135 if (_label.isEmpty()) {
136
137 switch (errorData.userType()) {
138 case QMetaType::QDate:
139 error =
140 c->translate("Cutelyst::ValidatorBefore", "Has to be before %1.")
141 .arg(errorData.toDate().toString(c->locale().dateFormat(QLocale::ShortFormat)));
142 break;
143 case QMetaType::QDateTime:
144 error = c->translate("Cutelyst::ValidatorBefore", "Has to be before %1.")
145 .arg(errorData.toDateTime().toString(
146 c->locale().dateTimeFormat(QLocale::ShortFormat)));
147 break;
148 case QMetaType::QTime:
149 error =
150 c->translate("Cutelyst::ValidatorBefore", "Has to be before %1.")
151 .arg(errorData.toTime().toString(c->locale().timeFormat(QLocale::ShortFormat)));
152 break;
153 default:
154 error = validationDataError(c);
155 break;
156 }
157
158 } else {
159
160 switch (errorData.userType()) {
161 case QMetaType::QDate:
162 error =
163 c->translate("Cutelyst::ValidatorBefore",
164 "The date in the “%1” field must be before %2.")
165 .arg(_label,
166 errorData.toDate().toString(c->locale().dateFormat(QLocale::ShortFormat)));
167 break;
168 case QMetaType::QDateTime:
169 error = c->translate("Cutelyst::ValidatorBefore",
170 "The date and time in the “%1” field must be before %2.")
171 .arg(_label,
172 errorData.toDateTime().toString(
173 c->locale().dateTimeFormat(QLocale::ShortFormat)));
174 break;
175 case QMetaType::QTime:
176 error =
177 c->translate("Cutelyst::ValidatorBefore",
178 "The time in the “%1” field must be before %2.")
179 .arg(_label,
180 errorData.toTime().toString(c->locale().timeFormat(QLocale::ShortFormat)));
181 break;
182 default:
183 error = validationDataError(c);
184 break;
185 }
186 }
187
188 return error;
189}
190
191QString ValidatorBefore::genericValidationDataError(Context *c, const QVariant &errorData) const
192{
193 QString error;
194
195 Q_UNUSED(errorData)
196 error =
197 c->translate("Cutelyst::ValidatorBefore",
198 "The comparison value is not a valid date and/or time, or cannot be found.");
199
200 return error;
201}
202
203QString ValidatorBefore::genericParsingError(Context *c, const QVariant &errorData) const
204{
205 QString error;
206
207 Q_D(const ValidatorBefore);
208
209 const QString _label = label(c);
210 if (d->inputFormat) {
211 if (_label.isEmpty()) {
212 //: %1 will be replaced by the datetime format
213 error =
214 c->translate(
215 "Cutelyst::ValidatorBefore",
216 "Could not be parsed according to the following date and/or time format: %1")
217 .arg(c->translate(d->translationContext.data(), d->inputFormat));
218 } else {
219 //: %1 will be replaced by the field label, %2 will be replaced by the datetime format
220 error = c->translate("Cutelyst::ValidatorBefore",
221 "The value of the “%1” field could not be parsed according to the "
222 "following date and/or time format: %2")
223 .arg(_label, c->translate(d->translationContext.data(), d->inputFormat));
224 }
225 } else {
226
227 if (_label.isEmpty()) {
228 switch (errorData.userType()) {
229 case QMetaType::QDateTime:
230 error = c->translate("Cutelyst::ValidatorBefore",
231 "Could not be parsed as date and time.");
232 break;
233 case QMetaType::QTime:
234 error = c->translate("Cutelyst::ValidatorBefore", "Could not be parsed as time.");
235 break;
236 case QMetaType::QDate:
237 error = c->translate("Cutelyst::ValidatorBefore", "Could not be parsed as date.");
238 break;
239 default:
240 error = validationDataError(c);
241 break;
242 }
243 } else {
244 switch (errorData.userType()) {
245 case QMetaType::QDateTime:
246 //: %1 will be replaced by the field label
247 error = c->translate(
248 "Cutelyst::ValidatorBefore",
249 "The value in the “%1” field could not be parsed as date and time.")
250 .arg(_label);
251 break;
252 case QMetaType::QTime:
253 //: %1 will be replaced by the field label
254 error = c->translate("Cutelyst::ValidatorBefore",
255 "The value in the “%1” field could not be parsed as time.")
256 .arg(_label);
257 break;
258 case QMetaType::QDate:
259 //: %1 will be replaced by the field label
260 error = c->translate("Cutelyst::ValidatorBefore",
261 "The value in the “%1” field could not be parsed as date.")
262 .arg(_label);
263 break;
264 default:
265 error = validationDataError(c);
266 break;
267 }
268 }
269 }
270
271 return error;
272}
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 before a comparison value.
QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if comparison data was invalid.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
~ValidatorBefore() override
Deconstructs the before validator.
QString genericParsingError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if the input value could not be parsed.
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