cutelyst 4.3.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
13 const QVariant &comparison,
14 const QString &timeZone,
15 const char *inputFormat,
16 const ValidatorMessages &messages,
17 const QString &defValKey)
18 : ValidatorRule(*new ValidatorBeforePrivate(field,
19 comparison,
20 timeZone,
21 inputFormat,
22 messages,
23 defValKey))
24{
25}
26
28
30{
32
33 Q_D(const ValidatorBefore);
34
35 const QString v = value(params);
36
37 if (!v.isEmpty()) {
38
39 const QTimeZone tz = d->extractTimeZone(c, params, d->timeZone);
40
41 const QVariant _comp =
42 (d->comparison.userType() == QMetaType::QString)
43 ? d->extractOtherDateTime(c, params, d->comparison.toString(), tz, d->inputFormat)
44 : d->comparison;
45
46 if (_comp.userType() == QMetaType::QDate) {
47
48 const QDate odate = _comp.toDate();
49 if (Q_UNLIKELY(!odate.isValid())) {
50 qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison date";
52 } else {
53 const QDate date = d->extractDate(c, v, d->inputFormat);
54 if (Q_UNLIKELY(!date.isValid())) {
55 qCWarning(C_VALIDATOR).noquote().nospace()
56 << debugString(c) << " Can not parse input date \"" << v << "\"";
57 result.errorMessage = parsingError(c, odate);
58 } else {
59 if (Q_UNLIKELY(date >= odate)) {
60 qCDebug(C_VALIDATOR).noquote()
61 << debugString(c) << "Input" << date << "is not before" << odate;
62 result.errorMessage = validationError(c, odate);
63 } else {
64 result.value.setValue(date);
65 }
66 }
67 }
68
69 } else if (_comp.userType() == QMetaType::QDateTime) {
70
71 const QDateTime odatetime = _comp.toDateTime();
72 if (Q_UNLIKELY(!odatetime.isValid())) {
73 qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison datetime";
75 } else {
76 const QDateTime datetime = d->extractDateTime(c, v, d->inputFormat, tz);
77 if (Q_UNLIKELY(!datetime.isValid())) {
78 qCWarning(C_VALIDATOR).noquote().nospace()
79 << debugString(c) << " Can not parse input datetime \"" << v << "\"";
80 result.errorMessage = parsingError(c, odatetime);
81 } else {
82 if (Q_UNLIKELY(datetime >= odatetime)) {
83 qCDebug(C_VALIDATOR).noquote() << debugString(c) << "Input" << datetime
84 << "is not before" << odatetime;
85 result.errorMessage = validationError(c, odatetime);
86 } else {
87 result.value.setValue(datetime);
88 }
89 }
90 }
91
92 } else if (_comp.userType() == QMetaType::QTime) {
93
94 const QTime otime = _comp.toTime();
95 if (Q_UNLIKELY(!otime.isValid())) {
96 qCWarning(C_VALIDATOR).noquote() << debugString(c) << "Invalid comparison time";
98 } else {
99 const QTime time = d->extractTime(c, v, d->inputFormat);
100 if (Q_UNLIKELY(!time.isValid())) {
101 qCWarning(C_VALIDATOR).noquote().nospace()
102 << debugString(c) << " Can not parse input time \"" << v << "\"";
103 result.errorMessage = parsingError(c, otime);
104 } else {
105 if (Q_UNLIKELY(time >= otime)) {
106 qCDebug(C_VALIDATOR).noquote()
107 << debugString(c) << "Input" << time << "is not before" << otime;
108 result.errorMessage = validationError(c, otime);
109 } else {
110 result.value.setValue(time);
111 }
112 }
113 }
114
115 } else {
116 qCWarning(C_VALIDATOR).noquote()
117 << debugString(c) << "Invalid comparison data:" << d->comparison;
119 }
120 } else {
121 defaultValue(c, &result);
122 }
123
124 return result;
125}
126
128 const QVariant &errorData) const
129{
130 const QString _label = label(c);
131 if (_label.isEmpty()) {
132
133 switch (errorData.userType()) {
134 case QMetaType::QDate:
135 //: %1 will be replaced by the comparison date in short format
136 //% "Has to be before %1."
137 return c->qtTrId("cutelyst-valbefore-genvalerr-date")
138 .arg(c->locale().toString(errorData.toDate(), QLocale::ShortFormat));
140 //: %1 will be replaced by the comparison datetime in short format
141 //% "Has to be before %1."
142 return c->qtTrId("cutelyst-valbefore-genvalerr-dt")
144 case QMetaType::QTime:
145 //: %1 will be replaced by the comparison time in short format
146 //% "Has to be before %1."
147 return c->qtTrId("cutelyst-valbefore-genvalerr-time")
148 .arg(c->locale().toString(errorData.toTime(), QLocale::ShortFormat));
149 default:
150 return validationDataError(c);
151 }
152
153 } else {
154
155 switch (errorData.userType()) {
156 case QMetaType::QDate:
157 //: %1 will be replaced by the field label, %2 by the comparison date in short format
158 //% "The date in the “%1” field must be before %2."
159 return c->qtTrId("cutelyst-valbefore-genvalerr-date-label")
160 .arg(_label, c->locale().toString(errorData.toDate(), QLocale::ShortFormat));
162 //: %1 will be replaced by the field label, %2 by the comparison datetime in short
163 //: format
164 //% "The date and time in the “%1” field must be before %2."
165 return c->qtTrId("cutelyst-valbefore-genvalerr-dt-label")
166 .arg(_label, c->locale().toString(errorData.toDateTime(), QLocale::ShortFormat));
167 case QMetaType::QTime:
168 //: %1 will be replaced by the field label, %2 by the comparison time in short format
169 //% "The time in the “%1” field must be before %2."
170 return c->qtTrId("cutelyst-valbefore-genvalerr-time-label")
171 .arg(_label, c->locale().toString(errorData.toTime(), QLocale::ShortFormat));
172 default:
173 return validationDataError(c);
174 }
175 }
176}
177
179{
180 Q_UNUSED(errorData)
181 const QString _label = label(c);
182 // the translation strings are defined in ValidatorAfter
183 if (_label.isEmpty()) {
184 return c->qtTrId("cutelyst-validator-genvaldataerr-dt");
185 } else {
186 return c->qtTrId("cutelyst-validator-genvaldataerr-dt-label").arg(_label);
187 }
188}
189
191{
192 Q_D(const ValidatorBefore);
193
194 // translation strings are defined in ValidatorAfter
195
196 const QString _label = label(c);
197 if (d->inputFormat) {
198 const QString _inputFormatTranslated =
199 d->translationContext ? c->translate(d->translationContext, d->inputFormat)
200 : c->qtTrId(d->inputFormat);
201 if (_label.isEmpty()) {
202 return c->qtTrId("cutelyst-validator-genparseerr-dt-format")
203 .arg(_inputFormatTranslated);
204 } else {
205 return c->qtTrId("cutelyst-validator-genparseerr-dt-format-label")
206 .arg(_label, _inputFormatTranslated);
207 }
208 } else {
209
210 if (_label.isEmpty()) {
211 switch (errorData.userType()) {
213 return c->qtTrId("cutelyst-validator-genparseerr-dt");
214 case QMetaType::QTime:
215 return c->qtTrId("cutelyst-validator-genparseerr-time");
216 case QMetaType::QDate:
217 return c->qtTrId("cutelyst-validator-genparseerr-date");
218 default:
219 return validationDataError(c);
220 }
221 } else {
222 switch (errorData.userType()) {
224 return c->qtTrId("cutelyst-vaidator-genparseerr-dt-label").arg(_label);
225 case QMetaType::QTime:
226 return c->qtTrId("cutelyst-validator-genparseerr-time-label").arg(_label);
227 case QMetaType::QDate:
228 return c->qtTrId("cutelyst-validator-genparseerr-date-label").arg(_label);
229 default:
230 return validationDataError(c);
231 }
232 }
233 }
234}
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 before a comparison value.
ValidatorBefore(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 genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
QString genericParsingError(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.