Cutelyst  3.5.0
validatortime.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatortime_p.h"
7 #include <QTime>
8 
9 using namespace Cutelyst;
10 
11 ValidatorTime::ValidatorTime(const QString &field, const char *format, const Cutelyst::ValidatorMessages &messages, const QString &defValKey) :
12  ValidatorRule(*new ValidatorTimePrivate(field, format, messages, defValKey))
13 {
14 }
15 
17 {
18 }
19 
21 {
22  ValidatorReturnType result;
23 
24  Q_D(const ValidatorTime);
25 
26  const QString v = value(params);
27 
28  if (!v.isEmpty()) {
29  const QTime time = d->extractTime(c, v, d->format);
30 
31  if (!time.isValid()) {
32  result.errorMessage = validationError(c);
33  qCDebug(C_VALIDATOR, "ValidatorTime: Validation failed for value \"%s\" in field %s at %s::%s: not a valid time", qPrintable(v), qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
34  } else {
35  result.value.setValue(time);
36  }
37 
38  } else {
39  defaultValue(c, &result, "ValidatorTime");
40  }
41 
42  return result;
43 }
44 
46 {
47  QString error;
48 
49  Q_D(const ValidatorTime);
50 
51  Q_UNUSED(errorData)
52 
53  const QString _label = label(c);
54 
55  if (_label.isEmpty()) {
56 
57  if (d->format) {
58  //: %1 will be replaced by the erquired time format
59  error = c->translate("Cutelyst::ValidatorTime", "Not a valid time according to the following date format: %1").arg(c->translate(d->translationContext.data(), d->format));
60  } else {
61  error = c->translate("Cutelyst::ValidatorTime", "Not a valid time.");
62  }
63 
64  } else {
65 
66  if (d->format) {
67  //: %1 will be replaced by the field label, %2 will be replaced by the required time format
68  error = c->translate("Cutelyst::ValidatorTime", "The value in the “%1” field can not be parsed as time according to the following scheme: %2").arg(_label, c->translate(d->translationContext.data(), d->format));
69  } else {
70  //: %1 will be replaced by the field label
71  error = c->translate("Cutelyst::ValidatorTime", "The value in the “%1” field can not be parsed as time.").arg(_label);
72  }
73  }
74 
75  return error;
76 }
ValidatorTime(const QString &field, const char *format=nullptr, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new time validator.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Stores custom error messages and the input field label.
bool isValid() const const
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
The Cutelyst Context.
Definition: context.h:38
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:471
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
~ValidatorTime() override
Deconstructs time the validator.
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Checks if the input data is a valid time.
Definition: validatortime.h:36
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 ...