cutelyst  3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatornumeric.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatornumeric_p.h"
7 
8 using namespace Cutelyst;
9 
11  const Cutelyst::ValidatorMessages &messages,
12  const QString &defValKey)
13  : ValidatorRule(*new ValidatorNumericPrivate(field, messages, defValKey))
14 {
15 }
16 
18 {
19 }
20 
22 {
23  ValidatorReturnType result;
24 
25  const QString v = value(params);
26 
27  if (!v.isEmpty()) {
28  bool ok = false;
29  const double _v = v.toDouble(&ok);
30  if (Q_LIKELY(ok)) {
31  result.value.setValue(_v);
32  } else {
33  qCDebug(C_VALIDATOR,
34  "ValidatorNumeric: Validation failed for field %s at %s::%s: can not convert "
35  "input value into a numeric value.",
36  qPrintable(field()),
37  qPrintable(c->controllerName()),
38  qPrintable(c->actionName()));
39  result.errorMessage = validationError(c);
40  }
41  } else {
42  defaultValue(c, &result, "ValidatorNumeric");
43  }
44 
45  return result;
46 }
47 
49 {
50  QString error;
51  Q_UNUSED(errorData)
52  const QString _label = label(c);
53  if (_label.isEmpty()) {
54  error =
55  c->translate("Cutelyst::ValidatorNumeric", "Must be numeric, like 1, -2.5 or 3.454e3.");
56  } else {
57  //: %1 will be replaced by the field label
58  error =
59  c->translate(
60  "Cutelyst::ValidatorNumeric",
61  "You have to enter a numeric value into the “%1” field, like 1, -2.5 or 3.454e3.")
62  .arg(_label);
63  }
64  return error;
65 }
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
~ValidatorNumeric() override
Deconstructs the numeric validator.
Stores custom error messages and the input field label.
ValidatorNumeric(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new numeric validator.
double toDouble(bool *ok) const const
The Cutelyst Context.
Definition: context.h:38
bool isEmpty() const const
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:490
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.
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
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 ...