6#include "validatorafter_p.h"
14 const QVariant &comparison,
15 const QString &timeZone,
16 const char *inputFormat,
18 const QString &defValKey)
20 *new ValidatorAfterPrivate(field, comparison, timeZone, inputFormat, messages, defValKey))
32 const QString v =
value(params);
36 const QTimeZone tz = d->extractTimeZone(c, params, d->timeZone);
38 const QVariant _comp =
39 (d->comparison.userType() == QMetaType::QString)
40 ? d->extractOtherDateTime(c, params, d->comparison.toString(), tz, d->inputFormat)
43 if (_comp.userType() == QMetaType::QDate) {
45 const QDate odate = _comp.toDate();
46 if (Q_UNLIKELY(!odate.isValid())) {
47 qCWarning(C_VALIDATOR).noquote() <<
debugString(c) <<
"Invalid comparison date";
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 <<
"\"";
56 if (Q_UNLIKELY(date <= odate)) {
57 qCDebug(C_VALIDATOR).noquote()
58 <<
debugString(c) <<
"Input" << date <<
"is not after" << odate;
61 result.
value.setValue(date);
66 }
else if (_comp.userType() == QMetaType::QDateTime) {
68 const QDateTime odatetime = _comp.toDateTime();
69 if (Q_UNLIKELY(!odatetime.isValid())) {
70 qCWarning(C_VALIDATOR).noquote() <<
debugString(c) <<
"Invalid comparison datetime";
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 <<
"\"";
79 if (Q_UNLIKELY(datetime <= odatetime)) {
80 qCDebug(C_VALIDATOR).noquote()
81 <<
debugString(c) <<
"Input" << datetime <<
"is not after" << odatetime;
84 result.
value.setValue(datetime);
89 }
else if (_comp.userType() == QMetaType::QTime) {
91 const QTime otime = _comp.toTime();
92 if (Q_UNLIKELY(!otime.isValid())) {
93 qCWarning(C_VALIDATOR).noquote() <<
debugString(c) <<
"Invalid comparison time";
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 <<
"\"";
102 if (Q_UNLIKELY(time <= otime)) {
103 qCDebug(C_VALIDATOR).noquote()
104 <<
debugString(c) <<
"Input" << time <<
"is not after" << otime;
107 result.
value.setValue(time);
113 qCWarning(C_VALIDATOR).noquote()
114 <<
debugString(c) <<
"Invalid comparison data:" << d->comparison;
125 const QVariant &errorData)
const
129 const QString _label =
label(c);
130 if (_label.isEmpty()) {
132 switch (errorData.userType()) {
133 case QMetaType::QDate:
135 c->
translate(
"Cutelyst::ValidatorAfter",
"Has to be after %1.")
136 .arg(errorData.toDate().toString(c->
locale().dateFormat(QLocale::ShortFormat)));
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)));
143 case QMetaType::QTime:
145 c->
translate(
"Cutelyst::ValidatorAfter",
"Has to be after %1.")
146 .arg(errorData.toTime().toString(c->
locale().timeFormat(QLocale::ShortFormat)));
155 switch (errorData.userType()) {
156 case QMetaType::QDate:
159 "The date in the “%1” field must be after %2.")
161 errorData.toDate().toString(c->
locale().dateFormat(QLocale::ShortFormat)));
163 case QMetaType::QDateTime:
164 error = c->
translate(
"Cutelyst::ValidatorAfter",
165 "The date and time in the “%1” field must be after %2.")
167 errorData.toDateTime().toString(
168 c->
locale().dateTimeFormat(QLocale::ShortFormat)));
170 case QMetaType::QTime:
173 "The time in the “%1” field must be after %2.")
175 errorData.toTime().toString(c->
locale().timeFormat(QLocale::ShortFormat)));
193 "The comparison value is not a valid date and/or time, or cannot be found.");
204 const QString _label =
label(c);
205 if (d->inputFormat) {
206 if (_label.isEmpty()) {
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));
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));
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.");
226 case QMetaType::QTime:
227 error = c->
translate(
"Cutelyst::ValidatorAfter",
"Could not be parsed as time.");
229 case QMetaType::QDate:
230 error = c->
translate(
"Cutelyst::ValidatorAfter",
"Could not be parsed as date.");
237 switch (errorData.userType()) {
238 case QMetaType::QDateTime:
241 "Cutelyst::ValidatorAfter",
242 "The value in the “%1” field could not be parsed as date and time.")
245 case QMetaType::QTime:
247 error = c->
translate(
"Cutelyst::ValidatorAfter",
248 "The value in the “%1” field could not be parsed as time.")
251 case QMetaType::QDate:
253 error = c->
translate(
"Cutelyst::ValidatorAfter",
254 "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 after a comparison value.
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) 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 ¶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.