cutelyst  5.0.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatortime.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2025 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatortime_p.h"
7 
8 #include <QTime>
9 
10 using namespace Cutelyst;
11 
13  const char *format,
14  const Cutelyst::ValidatorMessages &messages,
15  const QString &defValKey)
16  : ValidatorRule(*new ValidatorTimePrivate(field, format, messages, defValKey))
17 {
18 }
19 
21 
23 {
24  ValidatorReturnType result;
25 
26  Q_D(const ValidatorTime);
27 
28  const QString v = value(params);
29 
30  if (!v.isEmpty()) {
31  const QTime time = d->extractTime(c, v, d->format);
32 
33  if (!time.isValid()) {
34  result.errorMessage = validationError(c);
35  qCDebug(C_VALIDATOR).noquote().nospace()
36  << debugString(c) << " \"" << v << "\" is not a valid time";
37  } else {
38  result.value.setValue(time);
39  }
40 
41  } else {
42  defaultValue(c, &result);
43  }
44 
45  return result;
46 }
47 
49 {
50  cb(validate(c, params));
51 }
52 
54 {
55  Q_D(const ValidatorTime);
56  Q_UNUSED(errorData)
57  const QString _label = label(c);
58 
59  if (d->format) {
60  const QString _formatTranslated = d->translationContext
61  ? c->translate(d->translationContext, d->format)
62  : c->qtTrId(d->format);
63  if (_label.isEmpty()) {
64  //% "Not a valid time according to the following format: %1"
65  return c->qtTrId("cutelyst-valtime-genvalerr-format").arg(_formatTranslated);
66  } else {
67  //% "The value in the “%1” field can not be parsed as time according "
68  //% "to the following format: %2"
69  return c->qtTrId("cutelyst-valtime-genvalerr-format-label")
70  .arg(_label, _formatTranslated);
71  }
72  } else {
73  if (_label.isEmpty()) {
74  //% "Not a valid time."
75  return c->qtTrId("cutelyst-valtime-genvalerr");
76  } else {
77  //% "The value in the “%1” field can not be parsed as time."
78  return c->qtTrId("cutelyst-valtime-genvalerr-label").arg(_label);
79  }
80  }
81 }
Stores custom error messages and the input field label.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
The Cutelyst Context.
Definition: context.h:42
void defaultValue(Context *c, ValidatorReturnType *result) const
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:485
bool isValid(int h, int m, int s, int ms)
The Cutelyst namespace holds all public Cutelyst API.
QString debugString(const Context *c) const
Base class for all validator rules.
ValidatorTime(const QString &field, const char *format=nullptr, const ValidatorMessages &messages={}, const QString &defValKey={})
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
QString value(const ParamsMultiMap &params) const
QString label(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
Definition: validatorrule.h:82
QString validationError(Context *c, const QVariant &errorData={}) const
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:658
Contains the result of a single input parameter validation.
Definition: validatorrule.h:52
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
QString arg(Args &&... args) const const
Checks if the input data is a valid time.
Definition: validatortime.h:41
void setValue(QVariant &&value)