19 #include "validatorafter_p.h"
24 using namespace Cutelyst;
27 ValidatorRule(*new ValidatorAfterPrivate(field, comparison, timeZone, inputFormat, messages, defValKey))
46 const QTimeZone tz = d->extractTimeZone(c, params, d->timeZone);
48 const QVariant _comp = (d->comparison.type() == QVariant::String)
49 ? d->extractOtherDateTime(c, params, d->comparison.toString(), tz, d->inputFormat)
52 if (_comp.
type() == QVariant::Date) {
55 if (Q_UNLIKELY(!odate.
isValid())) {
56 qCWarning(C_VALIDATOR,
"ValidatorAfter: Invalid comparison date and time for field %s at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
59 const QDate date = d->extractDate(c, v, d->inputFormat);
60 if (Q_UNLIKELY(!date.
isValid())) {
61 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()));
64 if (Q_UNLIKELY(date <= odate)) {
65 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()));
73 }
else if (_comp.
type() == QVariant::DateTime) {
76 if (Q_UNLIKELY(!odatetime.
isValid())) {
77 qCWarning(C_VALIDATOR,
"ValidatorAfter: Invalid comparison date and time for field %s at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
80 const QDateTime datetime = d->extractDateTime(c, v, d->inputFormat, tz);
81 if (Q_UNLIKELY(!datetime.
isValid())) {
82 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()));
85 if (Q_UNLIKELY(datetime <= odatetime)) {
86 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()));
94 }
else if (_comp.
type() == QVariant::Time) {
97 if (Q_UNLIKELY(!otime.
isValid())) {
98 qCWarning(C_VALIDATOR,
"ValidatorAfter: Invalid comparison time for field %s at %s::%s.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
101 const QTime time = d->extractTime(c, v, d->inputFormat);
102 if (Q_UNLIKELY(!time.
isValid())) {
103 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()));
106 if (Q_UNLIKELY(time <= otime)) {
107 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()));
116 qCWarning(C_VALIDATOR) <<
"ValidatorAfter: Invalid validation data for field" <<
field() <<
"at" << c->controllerName() <<
"::" << c->actionName() <<
":" << d->comparison;
133 switch (errorData.
type()) {
137 case QVariant::DateTime:
150 switch(errorData.
type()) {
154 case QVariant::DateTime:
175 error = c->
translate(
"Cutelyst::ValidatorAfter",
"The comparison value is not a valid date and/or time, or cannot be found.");
187 if (d->inputFormat) {
189 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));
191 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));
196 switch (errorData.
type()) {
197 case QVariant::DateTime:
198 error = c->
translate(
"Cutelyst::ValidatorAfter",
"Could not be parsed as date and time.");
201 error = c->
translate(
"Cutelyst::ValidatorAfter",
"Could not be parsed as time.");
204 error = c->
translate(
"Cutelyst::ValidatorAfter",
"Could not be parsed as date.");
211 switch (errorData.
type()) {
212 case QVariant::DateTime:
214 error = c->
translate(
"Cutelyst::ValidatorAfter",
"The value in the “%1” field could not be parsed as date and time.").
arg(_label);
218 error = c->
translate(
"Cutelyst::ValidatorAfter",
"The value in the “%1” field could not be parsed as time.").
arg(_label);
222 error = c->
translate(
"Cutelyst::ValidatorAfter",
"The value in the “%1” field could not be parsed as date.").
arg(_label);
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
QString toString(Qt::DateFormat format) const
QString field() const
Returns the name of the field to validate.
QString toString(Qt::DateFormat format) const
QString toString(Qt::DateFormat format) const
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
Stores custom error messages and the input field label.
QDateTime toDateTime() const
Checks if a date, time or datetime is after a comparison value.
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
~ValidatorAfter() override
Deconstructs the after 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.
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.
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 ...
void setValue(const T &value)
QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if comparison data was invalid.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Contains the result of a single input parameter validation.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occured while parsing input.