6 #include "validatorafter_p.h"
14 ValidatorRule(*new ValidatorAfterPrivate(field, comparison, timeZone, inputFormat, messages, defValKey))
29 const QString v =
value(params);
33 const QTimeZone tz = d->extractTimeZone(c, params, d->timeZone);
35 const QVariant _comp = (d->comparison.userType() == QMetaType::QString)
36 ? d->extractOtherDateTime(c, params, d->comparison.toString(), tz, d->inputFormat)
39 if (_comp.userType() == QMetaType::QDate) {
41 const QDate odate = _comp.toDate();
42 if (Q_UNLIKELY(!odate.isValid())) {
43 qCWarning(C_VALIDATOR,
"ValidatorAfter: Invalid comparison date and time for field %s at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
46 const QDate date = d->extractDate(c, v, d->inputFormat);
47 if (Q_UNLIKELY(!date.isValid())) {
48 qCWarning(C_VALIDATOR,
"ValidatorAfter: Can not parse input date \"%s\" for field %s at %s::%s.", qPrintable(v), qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
51 if (Q_UNLIKELY(date <= odate)) {
52 qCDebug(C_VALIDATOR,
"ValidatorAfter: Validation failed at %s::%s for field %s: Input date \"%s\" is not after \"%s\".", qPrintable(c->controllerName()), qPrintable(c->actionName()), qPrintable(
field()), qPrintable(date.toString()), qPrintable(odate.toString()));
55 result.
value.setValue(date);
60 }
else if (_comp.userType() == QMetaType::QDateTime) {
62 const QDateTime odatetime = _comp.toDateTime();
63 if (Q_UNLIKELY(!odatetime.isValid())) {
64 qCWarning(C_VALIDATOR,
"ValidatorAfter: Invalid comparison date and time for field %s at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
67 const QDateTime datetime = d->extractDateTime(c, v, d->inputFormat, tz);
68 if (Q_UNLIKELY(!datetime.isValid())) {
69 qCWarning(C_VALIDATOR,
"ValidatorAfter: Can not parse input date and time \"%s\" for field %s at %s::%s.", qPrintable(v), qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
72 if (Q_UNLIKELY(datetime <= odatetime)) {
73 qCDebug(C_VALIDATOR,
"ValidatorAfter: Validation failed at %s::%s for field %s: Input date and time \"%s\" is not after \"%s\".", qPrintable(c->controllerName()), qPrintable(c->actionName()), qPrintable(
field()), qPrintable(datetime.toString()), qPrintable(odatetime.toString()));
76 result.
value.setValue(datetime);
81 }
else if (_comp.userType() == QMetaType::QTime) {
83 const QTime otime = _comp.toTime();
84 if (Q_UNLIKELY(!otime.isValid())) {
85 qCWarning(C_VALIDATOR,
"ValidatorAfter: Invalid comparison time for field %s at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
88 const QTime time = d->extractTime(c, v, d->inputFormat);
89 if (Q_UNLIKELY(!time.isValid())) {
90 qCWarning(C_VALIDATOR,
"ValidatorAfter: Can not parse input time \"%s\" for field %s at %s::%s.", qPrintable(v), qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
93 if (Q_UNLIKELY(time <= otime)) {
94 qCDebug(C_VALIDATOR,
"ValidatorAfter: Validation failed at %s::%s for field %s: Input time \"%s\" is not after \"%s\".", qPrintable(c->controllerName()), qPrintable(c->actionName()), qPrintable(
field()), qPrintable(time.toString()), qPrintable(otime.toString()));
97 result.
value.setValue(time);
103 qCWarning(C_VALIDATOR) <<
"ValidatorAfter: Invalid validation data for field" <<
field() <<
"at" << c->controllerName() <<
"::" << c->actionName() <<
":" << d->comparison;
117 const QString _label =
label(c);
118 if (_label.isEmpty()) {
120 switch (errorData.userType()) {
121 case QMetaType::QDate:
122 error = QStringLiteral(
"Has to be after %1.").arg(errorData.toDate().toString(c->
locale().dateFormat(QLocale::ShortFormat)));
124 case QMetaType::QDateTime:
125 error = QStringLiteral(
"Has to be after %1.").arg(errorData.toDateTime().toString(c->
locale().dateTimeFormat(QLocale::ShortFormat)));
127 case QMetaType::QTime:
128 error = QStringLiteral(
"Has to be after %1.").arg(errorData.toTime().toString(c->
locale().timeFormat(QLocale::ShortFormat)));
137 switch(errorData.userType()) {
138 case QMetaType::QDate:
139 error = c->
translate(
"Cutelyst::ValidatorAfter",
"The date in the “%1” field must be after %2.").arg(_label, errorData.toDate().toString(c->
locale().dateFormat(QLocale::ShortFormat)));
141 case QMetaType::QDateTime:
142 error = c->
translate(
"Cutelyst::ValidatorAfter",
"The date and time in the “%1” field must be after %2.").arg(_label, errorData.toDateTime().toString(c->
locale().dateTimeFormat(QLocale::ShortFormat)));
144 case QMetaType::QTime:
145 error = c->
translate(
"Cutelyst::ValidatorAfter",
"The time in the “%1” field must be after %2.").arg(_label, errorData.toTime().toString(c->
locale().timeFormat(QLocale::ShortFormat)));
162 error = c->
translate(
"Cutelyst::ValidatorAfter",
"The comparison value is not a valid date and/or time, or cannot be found.");
173 const QString _label =
label(c);
174 if (d->inputFormat) {
175 if (_label.isEmpty()) {
176 error = c->
translate(
"Cutelyst::ValidatorAfter",
"Could not be parsed according to the follwing date and/or time format: %1").arg(c->
translate(d->translationContext.data(), d->inputFormat));
178 error = c->
translate(
"Cutelyst::ValidatorAfter",
"The value of the “%1” field could not be parsed according to the follwing date and/or time format: %2").arg(_label, c->
translate(d->translationContext.data(), d->inputFormat));
182 if (_label.isEmpty()) {
183 switch (errorData.userType()) {
184 case QMetaType::QDateTime:
185 error = c->
translate(
"Cutelyst::ValidatorAfter",
"Could not be parsed as date and time.");
187 case QMetaType::QTime:
188 error = c->
translate(
"Cutelyst::ValidatorAfter",
"Could not be parsed as time.");
190 case QMetaType::QDate:
191 error = c->
translate(
"Cutelyst::ValidatorAfter",
"Could not be parsed as date.");
198 switch (errorData.userType()) {
199 case QMetaType::QDateTime:
201 error = c->
translate(
"Cutelyst::ValidatorAfter",
"The value in the “%1” field could not be parsed as date and time.").arg(_label);
203 case QMetaType::QTime:
205 error = c->
translate(
"Cutelyst::ValidatorAfter",
"The value in the “%1” field could not be parsed as time.").arg(_label);
207 case QMetaType::QDate:
209 error = c->
translate(
"Cutelyst::ValidatorAfter",
"The value in the “%1” field could not be parsed as date.").arg(_label);
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 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 occured 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.