6#include "validatorbefore_p.h"
8#include <QtCore/QLoggingCategory>
12Q_LOGGING_CATEGORY(C_VALIDATORBEFORE,
"cutelyst.utils.validator.before", QtWarningMsg)
15 const QVariant &comparison,
16 const QString &timeZone,
17 const
char *inputFormat,
19 const QString &defValKey)
37 const QString v =
value(params);
41 const QTimeZone tz = d->extractTimeZone(c, params, d->timeZone);
43 const QVariant _comp =
44 (d->comparison.userType() == QMetaType::QString)
45 ? d->extractOtherDateTime(c, params, d->comparison.toString(), tz, d->inputFormat)
48 if (_comp.userType() == QMetaType::QDate) {
50 const QDate odate = _comp.toDate();
51 if (Q_UNLIKELY(!odate.isValid())) {
52 qCWarning(C_VALIDATOR).noquote() <<
debugString(c) <<
"Invalid comparison date";
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 <<
"\"";
61 if (Q_UNLIKELY(date >= odate)) {
62 qCDebug(C_VALIDATOR).noquote()
63 <<
debugString(c) <<
"Input" << date <<
"is not before" << odate;
66 result.
value.setValue(date);
71 }
else if (_comp.userType() == QMetaType::QDateTime) {
73 const QDateTime odatetime = _comp.toDateTime();
74 if (Q_UNLIKELY(!odatetime.isValid())) {
75 qCWarning(C_VALIDATOR).noquote() <<
debugString(c) <<
"Invalid comparison datetime";
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 <<
"\"";
84 if (Q_UNLIKELY(datetime >= odatetime)) {
85 qCDebug(C_VALIDATOR).noquote() <<
debugString(c) <<
"Input" << datetime
86 <<
"is not before" << odatetime;
89 result.
value.setValue(datetime);
94 }
else if (_comp.userType() == QMetaType::QTime) {
96 const QTime otime = _comp.toTime();
97 if (Q_UNLIKELY(!otime.isValid())) {
98 qCWarning(C_VALIDATOR).noquote() <<
debugString(c) <<
"Invalid comparison time";
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 <<
"\"";
107 if (Q_UNLIKELY(time >= otime)) {
108 qCDebug(C_VALIDATOR).noquote()
109 <<
debugString(c) <<
"Input" << time <<
"is not before" << otime;
112 result.
value.setValue(time);
118 qCWarning(C_VALIDATOR).noquote()
119 <<
debugString(c) <<
"Invalid comparison data:" << d->comparison;
130 const QVariant &errorData)
const
134 const QString _label =
label(c);
135 if (_label.isEmpty()) {
137 switch (errorData.userType()) {
138 case QMetaType::QDate:
140 c->
translate(
"Cutelyst::ValidatorBefore",
"Has to be before %1.")
141 .arg(errorData.toDate().toString(c->
locale().dateFormat(QLocale::ShortFormat)));
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)));
148 case QMetaType::QTime:
150 c->
translate(
"Cutelyst::ValidatorBefore",
"Has to be before %1.")
151 .arg(errorData.toTime().toString(c->
locale().timeFormat(QLocale::ShortFormat)));
160 switch (errorData.userType()) {
161 case QMetaType::QDate:
163 c->
translate(
"Cutelyst::ValidatorBefore",
164 "The date in the “%1” field must be before %2.")
166 errorData.toDate().toString(c->
locale().dateFormat(QLocale::ShortFormat)));
168 case QMetaType::QDateTime:
169 error = c->
translate(
"Cutelyst::ValidatorBefore",
170 "The date and time in the “%1” field must be before %2.")
172 errorData.toDateTime().toString(
173 c->
locale().dateTimeFormat(QLocale::ShortFormat)));
175 case QMetaType::QTime:
177 c->
translate(
"Cutelyst::ValidatorBefore",
178 "The time in the “%1” field must be before %2.")
180 errorData.toTime().toString(c->
locale().timeFormat(QLocale::ShortFormat)));
197 c->
translate(
"Cutelyst::ValidatorBefore",
198 "The comparison value is not a valid date and/or time, or cannot be found.");
209 const QString _label =
label(c);
210 if (d->inputFormat) {
211 if (_label.isEmpty()) {
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));
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));
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.");
233 case QMetaType::QTime:
234 error = c->
translate(
"Cutelyst::ValidatorBefore",
"Could not be parsed as time.");
236 case QMetaType::QDate:
237 error = c->
translate(
"Cutelyst::ValidatorBefore",
"Could not be parsed as date.");
244 switch (errorData.userType()) {
245 case QMetaType::QDateTime:
248 "Cutelyst::ValidatorBefore",
249 "The value in the “%1” field could not be parsed as date and time.")
252 case QMetaType::QTime:
254 error = c->
translate(
"Cutelyst::ValidatorBefore",
255 "The value in the “%1” field could not be parsed as time.")
258 case QMetaType::QDate:
260 error = c->
translate(
"Cutelyst::ValidatorBefore",
261 "The value in the “%1” field could not be parsed as date.")
QLocale locale() const noexcept
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
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 ¶ms) 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 ¶ms) 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.
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.