cutelyst  3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrequiredunlessstash.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2018-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorrequiredunlessstash_p.h"
7 
8 using namespace Cutelyst;
9 
11  const QString &stashKey,
12  const QVariantList &stashValues,
13  const ValidatorMessages &messages)
14  : ValidatorRule(
15  *new ValidatorRequiredUnlessStashPrivate(field, stashKey, stashValues, messages))
16 {
17 }
18 
20 {
21 }
22 
24  const ParamsMultiMap &params) const
25 {
26  ValidatorReturnType result;
27 
29 
30  if (d->stashKey.isEmpty() || d->stashValues.empty()) {
32  qCWarning(C_VALIDATOR,
33  "ValidatorRequiredUnlessStash: invalid validation data for field %s at %s::%s",
34  qPrintable(field()),
35  qPrintable(c->controllerName()),
36  qPrintable(c->actionName()));
37  } else {
38  const QString v = value(params);
39  const QVariant sv = c->stash(d->stashKey);
40  if (!d->stashValues.contains(sv)) {
41  if (!v.isEmpty()) {
42  result.value.setValue(v);
43  } else {
44  result.errorMessage = validationError(c);
45  }
46  } else {
47  if (!v.isEmpty()) {
48  result.value.setValue(v);
49  }
50  }
51  }
52 
53  return result;
54 }
55 
57  const QVariant &errorData) const
58 {
59  QString error;
60  Q_UNUSED(errorData)
61  const QString _label = label(c);
62  if (_label.isEmpty()) {
63  error = c->translate("Cutelyst::ValidatorRequiredUnlessStash", "This is required.");
64  } else {
65  //: %1 will be replaced by the field label
66  error =
67  c->translate("Cutelyst::ValidatorRequiredUnlessStash", "The “%1” field is required.")
68  .arg(_label);
69  }
70  return error;
71 }
ValidatorRequiredUnlessStash(const QString &field, const QString &stashKey, const QVariantList &stashValues, const ValidatorMessages &messages=ValidatorMessages())
Constructs a new required unless stash validator.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Stores custom error messages and the input field label.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
The Cutelyst Context.
Definition: context.h:38
void stash(const QVariantHash &unite)
Definition: context.cpp:566
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:490
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
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
<Cutelyst/Plugins/Utils/validatorrequiredunlessstash.h>
QString field() const
Returns the name of the field to validate.
~ValidatorRequiredUnlessStash() override
Deconstructs the required unless stash validator.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49