cutelyst  3.7.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrequiredifstash.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2018-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorrequiredifstash_p.h"
7 
8 using namespace Cutelyst;
9 
10 ValidatorRequiredIfStash::ValidatorRequiredIfStash(const QString &field, const QString &stashKey, const QVariantList &stashValues, const ValidatorMessages &messages) :
11  ValidatorRule(* new ValidatorRequiredIfStashPrivate(field, stashKey, stashValues, messages))
12 {
13 }
14 
16 {
17 }
18 
20 {
21  ValidatorReturnType result;
22 
23  Q_D(const ValidatorRequiredIfStash);
24 
25  if (d->stashKey.isEmpty() || d->stashValues.empty()) {
27  qCWarning(C_VALIDATOR, "ValidatorRequiredIfStash: invalid validation data for field %s at %s::%s", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
28  } else {
29  const QString v = value(params);
30  const QVariant sv = c->stash(d->stashKey);
31  if (d->stashValues.contains(sv)) {
32  if (v.isEmpty()) {
33  result.errorMessage = validationError(c);
34  qCDebug(C_VALIDATOR, "ValidatorRequiredIfStash: Validation failed for field %s at %s::%s", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
35  } else {
36  result.value.setValue(v);
37  }
38  } else {
39  if (!v.isEmpty()) {
40  result.value.setValue(v);
41  }
42  }
43  }
44 
45  return result;
46 }
47 
48 QString ValidatorRequiredIfStash::genericValidationError(Context *c, const QVariant &errorData) const
49 {
50  QString error;
51  Q_UNUSED(errorData)
52  const QString _label = label(c);
53  if (_label.isEmpty()) {
54  error = c->translate("Cutelyst::ValidatorRequiredIfStash", "This is required.");
55  } else {
56  //: %1 will be replaced by the field label
57  error = c->translate("Cutelyst::ValidatorRequiredIfStash", "The “%1” field is required.").arg(_label);
58  }
59  return error;
60 }
The Cutelyst Context.
Definition: context.h:39
void stash(const QVariantHash &unite)
Definition: context.cpp:546
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
The field under validation must be present and not empty if the content of a stash key is equal to on...
~ValidatorRequiredIfStash() override
Deconstructs the required if validator.
ValidatorRequiredIfStash(const QString &field, const QString &stashKey, const QVariantList &stashValues, const ValidatorMessages &messages=ValidatorMessages())
Constructs a new required if stash validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString field() const
Returns the name of the field to validate.
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49