Cutelyst  3.5.0
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 
10 ValidatorNumeric::ValidatorNumeric(const QString &field, const Cutelyst::ValidatorMessages &messages, const QString &defValKey) :
11  ValidatorRule(*new ValidatorNumericPrivate(field, messages, defValKey))
12 {
13 }
14 
16 {
17 }
18 
20 {
21  ValidatorReturnType result;
22 
23  const QString v = value(params);
24 
25  if (!v.isEmpty()) {
26  bool ok = false;
27  const double _v = v.toDouble(&ok);
28  if (Q_LIKELY(ok)) {
29  result.value.setValue(_v);
30  } else {
31  qCDebug(C_VALIDATOR, "ValidatorNumeric: Validation failed for field %s at %s::%s: can not convert input value into a numeric value.", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
32  result.errorMessage = validationError(c);
33  }
34  } else {
35  defaultValue(c, &result, "ValidatorNumeric");
36  }
37 
38  return result;
39 }
40 
42 {
43  QString error;
44  Q_UNUSED(errorData)
45  const QString _label = label(c);
46  if (_label.isEmpty()) {
47  error = c->translate("Cutelyst::ValidatorNumeric", "Must be numeric, like 1, -2.5 or 3.454e3.");
48  } else {
49  //: %1 will be replaced by the field label
50  error = c->translate("Cutelyst::ValidatorNumeric", "You have to enter a numeric value into the “%1” field, like 1, -2.5 or 3.454e3.").arg(_label);
51  }
52  return error;
53 }
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: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.
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
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
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
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 ...