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)
39 const QString v =
value(params);
43 const QTimeZone tz = d->extractTimeZone(c, params, d->timeZone);
45 const QVariant _comp =
46 (d->comparison.userType() == QMetaType::QString)
47 ? d->extractOtherDateTime(c, params, d->comparison.toString(), tz, d->inputFormat)
50 if (_comp.userType() == QMetaType::QDate) {
52 const QDate odate = _comp.toDate();
53 if (Q_UNLIKELY(!odate.isValid())) {
56 "ValidatorBefore: Invalid comparison date and time for field %s at %s::%s.",
58 qPrintable(c->controllerName()),
59 qPrintable(c->actionName()));
62 const QDate date = d->extractDate(c, v, d->inputFormat);
63 if (Q_UNLIKELY(!date.isValid())) {
66 "ValidatorBefore: Can not parse input date \"%s\" for field %s at %s::%s.",
69 qPrintable(c->controllerName()),
70 qPrintable(c->actionName()));
73 if (Q_UNLIKELY(date >= odate)) {
75 "ValidatorBefore: Validation failed at %s::%s for field %s: Input "
76 "date \"%s\" is not before \"%s\".",
77 qPrintable(c->controllerName()),
78 qPrintable(c->actionName()),
80 qPrintable(date.toString()),
81 qPrintable(odate.toString()));
84 result.
value.setValue(date);
89 }
else if (_comp.userType() == QMetaType::QDateTime) {
91 const QDateTime odatetime = _comp.toDateTime();
92 if (Q_UNLIKELY(!odatetime.isValid())) {
95 "ValidatorBefore: Invalid comparison date and time for field %s at %s::%s.",
97 qPrintable(c->controllerName()),
98 qPrintable(c->actionName()));
101 const QDateTime datetime = d->extractDateTime(c, v, d->inputFormat, tz);
102 if (Q_UNLIKELY(!datetime.isValid())) {
103 qCWarning(C_VALIDATOR,
104 "ValidatorBefore: Can not parse input date and time \"%s\" for field "
108 qPrintable(c->controllerName()),
109 qPrintable(c->actionName()));
112 if (Q_UNLIKELY(datetime >= odatetime)) {
114 "ValidatorBefore: Validation failed at %s::%s for field %s: Input "
115 "date and time \"%s\" is not before \"%s\".",
116 qPrintable(c->controllerName()),
117 qPrintable(c->actionName()),
119 qPrintable(datetime.toString()),
120 qPrintable(odatetime.toString()));
123 result.
value.setValue(datetime);
128 }
else if (_comp.userType() == QMetaType::QTime) {
130 const QTime otime = _comp.toTime();
131 if (Q_UNLIKELY(!otime.isValid())) {
132 qCWarning(C_VALIDATOR,
133 "ValidatorBefore: Invalid comparison time for field %s at %s::%s.",
135 qPrintable(c->controllerName()),
136 qPrintable(c->actionName()));
139 const QTime time = d->extractTime(c, v, d->inputFormat);
140 if (Q_UNLIKELY(!time.isValid())) {
143 "ValidatorBefore: Can not parse input time \"%s\" for field %s at %s::%s.",
146 qPrintable(c->controllerName()),
147 qPrintable(c->actionName()));
150 if (Q_UNLIKELY(time >= otime)) {
152 "ValidatorBefore: Validation failed at %s::%s for field %s: Input "
153 "time \"%s\" is not before \"%s\".",
154 qPrintable(c->controllerName()),
155 qPrintable(c->actionName()),
157 qPrintable(time.toString()),
158 qPrintable(otime.toString()));
161 result.
value.setValue(time);
167 qCWarning(C_VALIDATOR)
168 <<
"ValidatorBefore: Invalid validation data for field" <<
field() <<
"at"
169 << c->controllerName() <<
"::" << c->actionName() <<
":" << d->comparison;
180 const QVariant &errorData)
const
184 const QString _label =
label(c);
185 if (_label.isEmpty()) {
187 switch (errorData.userType()) {
188 case QMetaType::QDate:
190 QStringLiteral(
"Has to be before %1.")
191 .arg(errorData.toDate().toString(c->
locale().dateFormat(QLocale::ShortFormat)));
193 case QMetaType::QDateTime:
194 error = QStringLiteral(
"Has to be before %1.")
195 .arg(errorData.toDateTime().toString(
196 c->
locale().dateTimeFormat(QLocale::ShortFormat)));
198 case QMetaType::QTime:
200 QStringLiteral(
"Has to be before %1.")
201 .arg(errorData.toTime().toString(c->
locale().timeFormat(QLocale::ShortFormat)));
210 switch (errorData.userType()) {
211 case QMetaType::QDate:
213 c->
translate(
"Cutelyst::ValidatorBefore",
214 "The date in the “%1” field must be before %2.")
216 errorData.toDate().toString(c->
locale().dateFormat(QLocale::ShortFormat)));
218 case QMetaType::QDateTime:
219 error = c->
translate(
"Cutelyst::ValidatorBefore",
220 "The date and time in the “%1” field must be before %2.")
222 errorData.toDateTime().toString(
223 c->
locale().dateTimeFormat(QLocale::ShortFormat)));
225 case QMetaType::QTime:
227 c->
translate(
"Cutelyst::ValidatorBefore",
228 "The time in the “%1” field must be before %2.")
230 errorData.toTime().toString(c->
locale().timeFormat(QLocale::ShortFormat)));
247 c->
translate(
"Cutelyst::ValidatorBefore",
248 "The comparison value is not a valid date and/or time, or cannot be found.");
259 const QString _label =
label(c);
260 if (d->inputFormat) {
261 if (_label.isEmpty()) {
265 "Cutelyst::ValidatorBefore",
266 "Could not be parsed according to the following date and/or time format: %1")
267 .arg(c->
translate(d->translationContext.data(), d->inputFormat));
270 error = c->
translate(
"Cutelyst::ValidatorBefore",
271 "The value of the “%1” field could not be parsed according to the "
272 "following date and/or time format: %2")
273 .arg(_label, c->
translate(d->translationContext.data(), d->inputFormat));
277 if (_label.isEmpty()) {
278 switch (errorData.userType()) {
279 case QMetaType::QDateTime:
280 error = c->
translate(
"Cutelyst::ValidatorBefore",
281 "Could not be parsed as date and time.");
283 case QMetaType::QTime:
284 error = c->
translate(
"Cutelyst::ValidatorBefore",
"Could not be parsed as time.");
286 case QMetaType::QDate:
287 error = c->
translate(
"Cutelyst::ValidatorBefore",
"Could not be parsed as date.");
294 switch (errorData.userType()) {
295 case QMetaType::QDateTime:
298 "Cutelyst::ValidatorBefore",
299 "The value in the “%1” field could not be parsed as date and time.")
302 case QMetaType::QTime:
304 error = c->
translate(
"Cutelyst::ValidatorBefore",
305 "The value in the “%1” field could not be parsed as time.")
308 case QMetaType::QDate:
310 error = c->
translate(
"Cutelyst::ValidatorBefore",
311 "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 field() const
Returns the name of the field to validate.
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 char *validatorName) 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.
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.