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)));
QLocale locale() const noexcept
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
ValidatorBefore(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 before validator.